【问题标题】:Show / hide div on page load depending on radio button value - Jquery and Ruby on Rails Simple Form根据单选按钮值在页面加载时显示/隐藏 div - Jquery 和 Ruby on Rails 简单表单
【发布时间】: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


【解决方案1】:

上面的代码为单选按钮上的change 事件添加了一个事件处理程序。除此之外,您需要在页面加载时检查:checked 单选按钮的值,并据此显示/隐藏。

注意:为防止闪烁,请为元素指定初始样式display: none

jQuery(document).ready(function() {

	if ( jQuery('[name="user[nurse_attributes][qualified]"]:checked').val() !== 'true' ) {
		jQuery('#denied-quote-one').show();
	}
  	
  	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();
   		}
  	});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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: none;">
    <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>

【讨论】:

  • 感谢您的回答。我实际上已经尝试过你之前提到的内容,但是在页面加载时 div 仍然显示,我认为当时我的代码一定有问题。也许是其他原因造成的?将继续研究它。
  • 页面加载时好像没有注册真实值,很奇怪。
  • 你能贴出单选按钮 HMTL 吗?
  • 添加到我的问题
  • 更新了我的答案 - 您想要单选按钮的值 :checked
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 2013-05-17
相关资源
最近更新 更多