简体   繁体   English

未选中时 jQuery 单选按钮更改

[英]jQuery radio button change when unchecked

I'm trying to toggle a radio button with Rails & jQuery.我正在尝试使用 Rails 和 jQuery 切换单选按钮。 Works great with checkboxes but not with radio buttons.适用于复选框,但不适用于单选按钮。

Basically it does not refresh if billable is unchecked如果未选中 billable ,基本上它不会刷新

<%= f.radio_button :billable, 'true', :checked => true, :id => 'billable' %> This is a Billable Project <br />
<%= f.radio_button :billable, 'false' %> This is an Internal Project

<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
    $("#billable").change(function() { 
      $("#billing").toggle();
    } );
  });
</script>

by setting :checked => true its forced to always be true 通过设置:checked => true它被强制始终为true

for correctly setting the default value in controller 用于在控制器中正确设置默认值

def new
  @event = Event.new(:billable => true)
end

then in view 然后来看

<%= f.radio_button :billable, 'true' %> This is a Billable Project <br />
<%= f.radio_button :billable, 'false' %> This is an Internal Project

Try this 尝试这个

$(document).ready(function(){
    $("#billable").click(function() { 
      $("#billing").toggle(this.checked);
    } );
  });

This is what you want. 这就是你想要的。

  $(document).ready(function(){
    $("input[name=billable]").change(function() { 
      $("#billing").toggle();
    } );
  });

http://jsfiddle.net/AlienWebguy/KJ9kE/ http://jsfiddle.net/AlienWebguy/KJ9kE/

Try this please请试试这个

 var whichChecked = "";
 $('.radio').on('change', function() {

// if radio is checked, decrement variable associated with radio's value
if($(this).is(':checked')) {        
    if ($(this).val() === "int1") {
        int1--;
    }
    if ($(this).val() === "int2") {
        int2--;
    }
    if ($(this).val() === "int3") {
        int3--;
    }
    if ($(this).val() === "int4") {
        int4--;
    }       

    whichChecked = $(this).val();
}
// when unchecked, increment variable associated with radio's value, doesn't work!

if (whichChecked === "int1") {
    int1++;
}
if (whichChecked === "int2") {
    int2++;
}
if (whichChecked === "int3") {
    int3++;
}
if (whichChecked === "int4") {
    int4--;
} 

});

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

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