0

ラジオボタンがチェックされたことを検出するにはどうすればよいですか?

HTML :

<label><input type="radio" name="test">One</label>
<label><input type="radio" name="test">Two</label>

ユーザーがラジオボタンからオプションを選択するたびに特定の機能を実行したい

私は次のようなものを試しました:

$("input:radio").on("change", function(){
    if($(this).is(':checked')) {
        $(this).attr('checked', false)
        // do some more...

    }
});

デモ: http://jsfiddle.net/A5EzU/

「クリア」デバッグステートメントがフィドルに表示されるように、一度クリックすると、常にクリックされているように見えます。

4

2 に答える 2

1

一度に 1 つしかチェックできないラジオ ボタンではなく、チェックボックスを試してみてください。

HTML:

<label><input type="checkbox" />One</label>
<label><input type="checkbox" />Two</label>

JS:

$("input:checkbox").on("change", function(){
  $("input:checkbox").not($(this)).prop('checked', false);
});

フィドルの例:クリック

于 2013-05-02T08:11:40.933 に答える
-1

これを試して

HTML

<label><input type="radio" name="test" id="one">One</label>
<label><input type="radio" name="test" id="two">Two</label>

<code></code>

JS

$("input:radio").on("change", function(){
if($(this).is(':checked')) {
    $('code').html('clear !');
    $(this).attr("id");
    $('code').append($(this).attr("id"));
    $(this).attr(':checked', false)
    if($(this).attr("id")=="one")
    {
        alert("One");
    }
    else
        alert("two");
  }
});

デモ

于 2013-05-02T08:02:12.920 に答える