简体   繁体   中英

How can I prevent a change event from being fired from a selected radio button?

I'm trying to fire a change event for a selected group radio button but this event gets fired if I select an already selected radio button. Can I prevent this from happening?

$("input[type='radio']").mouseup(function(){
    ... 
}).change(function(){
    ...
});

HTML

<label class="radio">
    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
            Option one
</label>
<label class="radio">
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
            Option two
</label>
$("input[type='radio']:not(:selected)").mouseup(function(){
    ... 
}).change(function(){
    ...
});

OR

$("input[type='radio']:not(:checked)").mouseup(function(){
    ... 
}).change(function(){
    ...
});

:not(:selected) will exclude all selected radio s.

Read about :selected and :not() selectors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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