admin管理员组

文章数量:1420166

I was trying to solve this problem, but my reset button just can't uncheck the radio button..... :( Hope experts here can help me to solve my problem. Thx!!!!

just created the fiddle /

<input type="radio" class="radio" name="r[][9]" id="id_19" value="1|9">
                                <label for="id_19">Choice 1</label>
<input type="radio" class="radio" name="r[][9]" id="id_29" value="2|9">
                                <label for="id_29">Choice 1</label>
<button class="reset1" value="9">Reset</button>


$(document).ready(function(){
    $(".radio")
        .button();

    $(".reset1")
        .button()
        .click(function(){
            var radio = $('[name="r[][9]"]');
            radio.prop('checked',false);
            return false;
        })
});

I was trying to solve this problem, but my reset button just can't uncheck the radio button..... :( Hope experts here can help me to solve my problem. Thx!!!!

just created the fiddle http://jsfiddle/8GLPC/

<input type="radio" class="radio" name="r[][9]" id="id_19" value="1|9">
                                <label for="id_19">Choice 1</label>
<input type="radio" class="radio" name="r[][9]" id="id_29" value="2|9">
                                <label for="id_29">Choice 1</label>
<button class="reset1" value="9">Reset</button>


$(document).ready(function(){
    $(".radio")
        .button();

    $(".reset1")
        .button()
        .click(function(){
            var radio = $('[name="r[][9]"]');
            radio.prop('checked',false);
            return false;
        })
});
Share Improve this question edited Jul 9, 2014 at 7:51 Michael Ngooi asked Jul 9, 2014 at 7:37 Michael NgooiMichael Ngooi 1315 silver badges11 bronze badges 2
  • it would be better if you create a Fiddle – Amit Soni Commented Jul 9, 2014 at 7:47
  • That fiddle does not have jQuery and jQuery UI enable (options on the left). Have added working answer below with JSFiddle. – iCollect.it Ltd Commented Jul 9, 2014 at 7:52
Add a ment  | 

2 Answers 2

Reset to default 6

You need to refresh the jQuery button state after changing the underlying radio buttons.

JSFiddle: http://jsfiddle/TrueBlueAussie/BPv7F/1/

$(document).ready(function(){
    $(".radio")
        .button();

    $(".reset1")
        .button()
        .click(function(){
            var radio = $('[name="r[][9]"]');
            radio.prop('checked', false).button("refresh");
            return false;
        })
});

jQuery UI Buttons create separate elements, so changing the radio button state has no immediate effect on them.

Reference: http://api.jqueryui./button/#method-refresh

If you view the DOM, in say Chrome's F12 DOM inspector, you will see button() creates a LABEL and SPAN preceding each radio button. The actual radio button is styled out with the class ui-helper-hidden-accessible

Try this:

<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>

<h3>A demonstration of how to uncheck a Checkbox</h3>

Checkbox: <input type="checkbox" class="myCheck" checked>
Checkbox2: <input type="checkbox" class="myCheck" >
Checkbox3: <input type="checkbox" class="myCheck" >
<p>Click the "Try it" button to check the checkbox.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
$(".myCheck").prop('checked' , false);
}
</script>

</body>

Basiclly, it unchecks all checkboxes with the class .myCheck

本文标签: javascriptjquery how to uncheck radio buttonStack Overflow