开发者

jQuery radio button change when unchecked

I'm trying to toggle a radio button with Rails & jQuery. Works great with checkboxes but not with radio buttons.

Basically it does not refresh if billable is unchecked

<%= 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/javas开发者_如何学Gocript" charset="utf-8">
  $(document).ready(function(){
    $("#billable").change(function() { 
      $("#billing").toggle();
    } );
  });
</script>


by setting :checked => true its forced to always be 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


This is what you want.

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

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--;
} 

});


Try this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜