See how to dynamically change a select menu based on another select menu using Javascript. In this episode everything is kept client side which leads to a more responsive user interface.
# javascripts_controller.rb
def dynamic_states
@states = State.find(:all)
end
# application_helper.rb
def javascript(*files)
content_for(:head) { javascript_include_tag(*files) }
end
def stylesheet(*files)
content_for(:head) { stylesheet_link_tag(*files) }
end
<!-- people/new.html.erb -->
<% javascript 'dynamic_states' %>
...
<p>
<label for="person_country_id">Country:</label>
<%= f.collection_select :country_id, Country.find(:all), :id, :name, :prompt => "Select a Country" %>
</p>
<p id="state_field">
<label for="person_state_id">State or Province:</label>
<%= f.collection_select :state_id, State.find(:all), :id, :name, :prompt => "Select a State" %>
</p>
// javascripts/dynamic_states.js.erb
var states = new Array();
<% for state in @states -%>
states.push(new Array(<%= state.country_id %>, '<%=h state.name %>', <%= state.id %>));
<% end -%>
function countrySelected() {
country_id = $('person_country_id').getValue();
options = $('person_state_id').options;
options.length = 1;
states.each(function(state) {
if (state[0] == country_id) {
options[options.length] = new Option(state[1], state[2]);
}
});
if (options.length == 1) {
$('state_field').hide();
} else {
$('state_field').show();
}
}
document.observe('dom:loaded', function() {
countrySelected();
$('person_country_id').observe('change', countrySelected);
});
分享到:
相关推荐
88. Insert A QuickTime Movie V1.0.1 For Adobe Dreamweaver 89. Insert Filename V1.0.0 For Adobe Dreamweaver 90. Insert Flash Animation V1.0.1 For Adobe Dreamweaver 91. javascript Syntax Checker V1.0.0 ...
88. Insert A QuickTime Movie V1.0.1 For Adobe Dreamweaver 89. Insert Filename V1.0.0 For Adobe Dreamweaver 90. Insert Flash Animation V1.0.1 For Adobe Dreamweaver 91. javascript Syntax Checker V1.0.0 ...
IE - Running Scripts From your Hard Drive Warning! If you have SP2 installed, by default, IE ... allow for dynamic content, such as menus to display on top of the Flash object. The wmode Parameter-
2. **选择测试应用**:接着,按下“Menu Select”键,并在菜单中选择“IMT-2000 Mobile Station”下的“CDMA2000 Cellular – Signaling”应用。 3. **设置网络标准**:使用“Network Standard”软键将网络标准设置...
88. Insert A QuickTime Movie V1.0.1 For Adobe Dreamweaver 89. Insert Filename V1.0.0 For Adobe Dreamweaver 90. Insert Flash Animation V1.0.1 For Adobe Dreamweaver 91. javascript Syntax Checker V1.0.0 ...
88. Insert A QuickTime Movie V1.0.1 For Adobe Dreamweaver 89. Insert Filename V1.0.0 For Adobe Dreamweaver 90. Insert Flash Animation V1.0.1 For Adobe Dreamweaver 91. javascript Syntax Checker V1.0.0 ...
88. Insert A QuickTime Movie V1.0.1 For Adobe Dreamweaver 89. Insert Filename V1.0.0 For Adobe Dreamweaver 90. Insert Flash Animation V1.0.1 For Adobe Dreamweaver 91. javascript Syntax Checker V1.0.0 ...
88. Insert A QuickTime Movie V1.0.1 For Adobe Dreamweaver 89. Insert Filename V1.0.0 For Adobe Dreamweaver 90. Insert Flash Animation V1.0.1 For Adobe Dreamweaver 91. javascript Syntax Checker V1.0.0 ...
88. Insert A QuickTime Movie V1.0.1 For Adobe Dreamweaver 89. Insert Filename V1.0.0 For Adobe Dreamweaver 90. Insert Flash Animation V1.0.1 For Adobe Dreamweaver 91. javascript Syntax Checker V1.0.0 ...
88. Insert A QuickTime Movie V1.0.1 For Adobe Dreamweaver 89. Insert Filename V1.0.0 For Adobe Dreamweaver 90. Insert Flash Animation V1.0.1 For Adobe Dreamweaver 91. javascript Syntax Checker V1.0.0 ...
The chapters walk you through the SQL SELECT statement, Access queries, data access components, and data binding.Chapters 17 and 18 provide an introduction to Dynamic Data Web Sites and ASP.NET and ...
private void AddChildNodes(TreeNode parentNode, dynamic item) { var childItems = from childItem in item.Descendants("MenuItem") select new { Name = childItem.Attribute("name").Value }; foreach ...
<select|dropdown>, fix of "change" event generation when popup is shown. "change" is postponed until popup dismissal. [editing] fix of VK_LEFT/VK_RIGHT handling. more responsive handling of set focus ...
04.zip Outlook Style UI Outlook风格的界面效果(16KB)<END><br>5,05.zip Dynamic child window repositioning 动态改变对话框的大小, 对话框中的控件相应改变(15KB)<END><br>6,06.zip Enhanced list...
owner-draw menus and much more. 2. The editors of standard properties with some advanced features. TPicture and TGraphic editor adds Copy and Paste Buttons, supports Icons in Clipboard, favorites ...
- GUI: select mouse capture toggle method in .bochsrc. - Ported most of Qemu's 'virtual VFAT' block driver (except runtime write support, but plus FAT32 suppport) - Added write protect option for ...
The selected text compare allows you to select portions of text between 2 files and execute a compare on ONLY the se Using the SSH/telnet console A tutorial for UltraEdit/UEStudio's SSH/telent ...
45.zip Get VersionInfo from resource file 从资源文件中得到程序的版本信息(1)(6KB)<END><br>46,dynip.zip A IP Poster for people with dynamic IP addresses(18KB)<END><br>47,cliptalk.zip An ...