jquery javascript:选择单选按钮时执行操作

Chr*_*ris 12 javascript jquery radio-button

我有以下单选按钮,默认情况下都没有选中.

<input type="radio" name="location" value="0" /> home
<input type="radio" name="location" value="1" /> work
<input type="radio" name="location" value="2" /> school
Run Code Online (Sandbox Code Playgroud)

如何检测其中任何一个被检查.我正在寻找像点击这样的东西,但它不起作用

$("input[name=location]").click(function(){
    alert("selected");
}); 
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 12

$('input[name=location]').change(function(){
    alert(this.checked);
});
Run Code Online (Sandbox Code Playgroud)


eKe*_*ek0 1

这对我来说效果很好。你引用jquery库了吗?

  • 看起来这是该行上方的一个错误,并且它弄乱了其下方的整个 javascript。 (2认同)