如何防止所选单选按钮触发更改事件

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

本文关键字:事件 单选按钮 何防止      更新时间:2023-11-08

我正试图为选定的组单选按钮触发一个更改事件,但如果我选择了一个已选定的单选按钮,则会触发此事件。我能阻止这种情况发生吗?

$("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(){
    ...
});

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

:not(:selected)将排除所有selected radio s。

阅读有关:selected:not()选择器的信息。