简体   繁体   English

Rails select_tag-更改事件时jQuery / JavaScript隐藏/显示

[英]Rails select_tag - jQuery/JavaScript hide/show on change event

I have the following in a helper: 我的助手中有以下内容:

def cancellation_policies_options_for_elite(room)
  options_for_select( [ [t("cancellation_policies.standard"), "standard"], [t("cancellation_policies.moderate"), "moderate"], [t("cancellation_policies.strict"), "strict"], [t("cancellation_policies.elite"), "elite"], [t("cancellation_policies.super_elite"), "super_elite"]] )
end

Which specifies both the text rendered in the select menu but also the value (ie standard, moderate etc.) 它既指定在选择菜单中呈现的文本,又指定值(即标准,中等等)。

In my view I then call: 在我看来,然后致电:

<%= select_tag('selected_cancellation_policy_id', cancellation_policies_options_for_elite(@room), id: 'select-policy', class: 'input-xxlarge') %>

Below this I have the following divs: 在此之下,我有以下div:

<div id="moderate" class="policies" style="display:none">
  Moderate policy
</div>

<div id="strict" class="policies" style="display:none">
  Strict policy
</div>

<div id="elite" class="policies" style="display:none">
  Elite policy
</div>

$("#select-policy").on('change', function() {
  alert("It's working!");
  $('.policies').hide();
  $("#" + $(this).val()).show();
}

I have jQuery and jQuery UI in my project and it is all compiling correctly as many other jQuery elements are working. 我的项目中有jQuery和jQuery UI,并且在许多其他jQuery元素正常工作时,它们都可以正确编译。 However I am getting no response at all from this function and completely missing why. 但是,我对此功能没有任何反应,完全不知道为什么。 Any ideas on this? 有什么想法吗?

This should work:- 这应该工作:

$(document).ready(function(){
    $('.policies').hide();
});

Then on change of select box do this:- 然后在更改选择框上执行以下操作:-

$(document).on('change', '#select-policy', function() {
    alert("It's working!");
    $('.policies').not("#" + $(this).val()).hide();
});

Also, in "Elite Policy" div, id should be "super_elite" instead of "elite". 另外,在“精英政策” div中,id应该是“ super_elite”而不是“ elite”。 Because in select box cancellation elite value is "super_elite". 因为在选择框中取消,精英值是“ super_elite”。 And standard policy div is missing here. 标准政策div在这里缺失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM