`
CaiDeHen
  • 浏览: 94288 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

#88 Dynamic Select Menus

阅读更多
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);
});
分享到:
评论

相关推荐

    Dreamweaver CS4 黄金插件10-08

    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 ...

    Dreamweaver CS4 黄金插件10-03

    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 ...

    最牛比的css菜单生成工具(收藏)-不下后悔一辈子

    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-

    CMU200_CDMA2000手机测试步骤

    2. **选择测试应用**:接着,按下“Menu Select”键,并在菜单中选择“IMT-2000 Mobile Station”下的“CDMA2000 Cellular – Signaling”应用。 3. **设置网络标准**:使用“Network Standard”软键将网络标准设置...

    Dreamweaver CS4 黄金插件10-02

    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 ...

    Dreamweaver CS4 黄金插件10-05

    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 ...

    Dreamweaver CS4 黄金插件10-04

    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 ...

    Dreamweaver CS4 黄金插件10-06

    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 ...

    Dreamweaver CS4 黄金插件10-07

    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 ...

    Dreamweaver CS4 黄金插件10-1

    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 ...

    beginning VisualBasic2010(英文版)入门必读

    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 ...

    使用XML填充TreeView菜单

    private void AddChildNodes(TreeNode parentNode, dynamic item) { var childItems = from childItem in item.Descendants("MenuItem") select new { Name = childItem.Attribute("name").Value }; foreach ...

    sciter-sdk-4.0.3.5348

    &lt;select|dropdown&gt;, 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 ...

    Visual C++ 编程资源大全(英文源码 表单)

    04.zip Outlook Style UI Outlook风格的界面效果(16KB)&lt;END&gt;&lt;br&gt;5,05.zip Dynamic child window repositioning 动态改变对话框的大小, 对话框中的控件相应改变(15KB)&lt;END&gt;&lt;br&gt;6,06.zip Enhanced list...

    RxLib控件包内含RxGIF,全部源码及DEMO

    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 ...

    Bochs - The cross platform IA-32 (x86) emulator

    - 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 ...

    UE(官方下载)

    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 ...

    Visual C++ 编程资源大全(英文源码 其它)

    45.zip Get VersionInfo from resource file 从资源文件中得到程序的版本信息(1)(6KB)&lt;END&gt;&lt;br&gt;46,dynip.zip A IP Poster for people with dynamic IP addresses(18KB)&lt;END&gt;&lt;br&gt;47,cliptalk.zip An ...

Global site tag (gtag.js) - Google Analytics