以编程方式选择无线电时,单选按钮更改事件未触发

SNa*_*Nag 6 jquery

在以下代码中,单击selectRadio2按钮不会触发无线电更改事件!为什么不?有解决方法吗?

HTML:

<input id="radio1" type="radio" name="radioGroup" value="One"/>
<label for="radio1">One</label><br/>
<input id="radio2" type="radio" name="radioGroup" value="Two"/>
<label for="radio2">Two</label><br/>
<input id="radio3" type="radio" name="radioGroup" value="Three"/>
<label for="radio3">Three</label>

<br/><br/>
<button id="selectradio2">Select Radio 2</button>
Run Code Online (Sandbox Code Playgroud)

JS:

$("input[name=radioGroup][type=radio]").change(function() {
    alert($(this).attr("id") + " checked");
});

$("#selectradio2").click(function() {
    $("#radio2").prop('checked',true);
});
Run Code Online (Sandbox Code Playgroud)

JSFIDDLE DEMO

Fel*_*lix 18

由于更改事件需要由用户而不是通过javascript代码发起的实际浏览器事件.您需要change使用以下命令触发事件:

$("#selectradio2").click(function() {
    $("#radio2").prop('checked',true).change(); // or trigger('change')
});
Run Code Online (Sandbox Code Playgroud)

更新小提琴