【发布时间】:2017-06-26 18:59:05
【问题描述】:
将 Jquery 与 Ruby on Rails 和 Simple Form Gem 结合使用,我可以在更改单选按钮时成功显示/隐藏 div,但是尽管进行了多次尝试,但我无法让它在页面加载时工作 - div 总是显示,无论单选按钮是真还是假。任何帮助都会非常感谢。
jQuery(document).ready(function() {
jQuery('[name="user[nurse_attributes][qualified]"]').on('change', function() {
if (jQuery(this).val() == 'true' ) {
jQuery('#denied-quote-one').hide();
} else {
jQuery('#denied-quote-one').show();
}
});
});
更新 - 单选按钮的 HTML 输出:
<div class="form-group radio_buttons optional user_nurse_qualified">
<input type="hidden" name="user[nurse_attributes][qualified]" value="">
<span class="radio">
<label for="user_nurse_attributes_qualified_true">
<input class="radio_buttons optional" type="radio" value="true" checked="checked" name="user[nurse_attributes][qualified]" id="user_nurse_attributes_qualified_true">Yes
</label>
</span>
<span class="radio">
<label for="user_nurse_attributes_qualified_false">
<input class="radio_buttons optional" readonly="readonly" type="radio" value="false" name="user[nurse_attributes][qualified]" id="user_nurse_attributes_qualified_false">No
</label>
</span>
</div>
<div id="denied-quote-one" style="display: block;">
<p class="p-red">Unfortunately we cannot give you a quote if your answer to this question is no. You will not be able to move forward.
</p>
</div>
【问题讨论】:
-
此代码仅在单选按钮的
change事件中触发 -
我知道,但我的问题是,如何让它在页面加载时触发?我尝试使用页面加载代替更改,但没有效果。
标签: javascript jquery ruby-on-rails ruby