javascript - Radio button onoff trigger works only one way - Stack Overflow

So this is the dumbest thing I've struggled with in awhile. I cannot get the state of a simple rad

So this is the dumbest thing I've struggled with in awhile. I cannot get the state of a simple radio button set to toggle something on the page.

<label for="pleteSw"><span>Completed?</span></label>
<input type="radio" id="pleteSw" name="pleteSw" value="1"/>Yes
<input type="radio" id="pleteSw" name="pleteSw" value="0" checked="checked"/>No<br/>

So you can see here an extremely simple yes/no radio button set to toggle an action. It needs to serve two purposes: to flag a yes/no value (1/0) in the POST data, and ideally trigger an action on the page using JS/jQuery. I'm having trouble with the latter.

The default state is "No"; if I click "Yes" I can retrieve an onchange or onclick event state and make something happen. However, this is a one-way switch; I cannot retrieve a state going back to the "No" selector once I've gone to "Yes". What I need to be able to do is show / hide an element on the page depending on what choice they've made in this radio set. If I click "Yes", I can trigger the action and see the page change. Once I click "No", however, it acts as if there was no state change and I cannot perform an action i.e. hide the element again.

I've tried variations on retrieving the "checked" state, the radio pair value, etc, e.g.

$("#pleteSw").change(function(e){
    alert( $(this).attr("checked") ); // only triggers when "Yes" is selected
});

Perhaps I should not be using a yes/no radio pair, but instead be using a single checkbox? Seems more user-friendly and elegant this way (radio buttons) to me.

So this is the dumbest thing I've struggled with in awhile. I cannot get the state of a simple radio button set to toggle something on the page.

<label for="pleteSw"><span>Completed?</span></label>
<input type="radio" id="pleteSw" name="pleteSw" value="1"/>Yes
<input type="radio" id="pleteSw" name="pleteSw" value="0" checked="checked"/>No<br/>

So you can see here an extremely simple yes/no radio button set to toggle an action. It needs to serve two purposes: to flag a yes/no value (1/0) in the POST data, and ideally trigger an action on the page using JS/jQuery. I'm having trouble with the latter.

The default state is "No"; if I click "Yes" I can retrieve an onchange or onclick event state and make something happen. However, this is a one-way switch; I cannot retrieve a state going back to the "No" selector once I've gone to "Yes". What I need to be able to do is show / hide an element on the page depending on what choice they've made in this radio set. If I click "Yes", I can trigger the action and see the page change. Once I click "No", however, it acts as if there was no state change and I cannot perform an action i.e. hide the element again.

I've tried variations on retrieving the "checked" state, the radio pair value, etc, e.g.

$("#pleteSw").change(function(e){
    alert( $(this).attr("checked") ); // only triggers when "Yes" is selected
});

Perhaps I should not be using a yes/no radio pair, but instead be using a single checkbox? Seems more user-friendly and elegant this way (radio buttons) to me.

Share Improve this question asked Oct 22, 2010 at 15:05 bdlbdl 1,50212 silver badges16 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

IDs must be unique, so it will only ever find the first one on your page. Use a class instead.

Really, ID's must be unique, but you don't need 2 ID's. You'll only monitor changes in one radio. For example - "Yes" value

<label for="pleteSw"><span>Completed?</span></label>
<input type="radio" id="pleteSw" name="pleteSw" value="1"/>Yes
<input type="radio" name="pleteSw" value="0" checked="checked"/>No<br/>

And the you'll process the checked attribute of only this element. True - "Yes", False - "No"

Some browsers don't do anything when alert(message), message=null. And since an unchecked field has no checked-attribute, that could be the thing :).

Try:

alert('Checked: '+$(this).attr("checked"));

This is separate, but you're kinda using the label wrong also. The label is meant to extend the click area so someone could click on the word 'Yes' and the radio button will activate. Hopefully this helps you out a little.

<span>Completed?</span>
<input type="radio" id="pleteSwYes" name="pleteSw" value="1"/><label for="pleteSwYes">Yes</label>
<input type="radio" id="pleteSwNo" name="pleteSw" value="0" checked="checked"/><label for="pleteSwNo">No</label><br/>

<script type="text/javascript" charset="utf-8">
    // If the radio button value is one then this evaluates to true.
    var pleteSW;
    jQuery("input[type='radio'][name='pleteSw']").change(function() {
        pleteSW = (jQuery(this).val() == 1);
        alert("pleteSW checked? " + pleteSW);
    });
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644230a4637874.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信