-1

チェックボックスのクリックですべてのラジオボタンをチェックまたはチェック解除する方法..チェックボックスがチェックされている場合は、すべてのラジオボタンもチェックされ、その逆も同様です..正しく機能していません

<input type="checkbox" id="Check" />SelectAll<br /><input type="radio"/>First<br />


    <input type="radio"/>Second<br />
            <input type="radio"/>Third<br />
            <input type="radio"/>Fourth<br />
            <input type="radio"/>Fifth</div>
            <script>
                var IsCheck = false;
                $(document).ready(function () {
                    $("#Check").change(function () {
                        if (IsCheck == false) {
                            $("input[type=radio]").attr("checked", true);
                            IsCheck == true
                        }
                        else { $("input[type=radio]").attr("checked", false); IsCheck == false }
                    });
                }); </script>
4

6 に答える 6

3

このステートメントでは、変数に代入するのではなく、オペランドを比較しているだけであることに注意してください。

IsCheck == true  
         ^------ REMOVE ONE = so it's an assignment

.attr("checked", true);また、次の正しい形式を使用しないでください。

$("input[type=radio]").attr("checked", 'checked'); //checking for jQuery < 1.6

そしてチェックを外す:

$("input[type=radio]").removeAttr("checked");  //unchecking for jQuery < 1.6

jQuery > 1.6 を使用している場合は、.prop()メソッドをブール値で使用できます。これは、使用しようとしていた方法と似ています。

$("input[type=radio]").prop("checked", true); //checking for jQuery >= 1.6
$("input[type=radio]").prop("checked", false); //unchecking for jQuery >= 1.6
于 2013-06-22T11:14:45.970 に答える
1

これを試して

 $(document).ready(function () {                    
    $("#Check").change(function () {                        
      $("input[type=radio]").attr("checked", $("#Check").is(":checked"));                            
    });
 });

デモ

于 2013-06-22T11:14:35.850 に答える
1

あなたの質問に対して、これが答えかもしれません:

$("#Check").change(function () {
    $("input:radio").prop("checked", this.checked);
});

デモ: http://jsfiddle.net/hungerpain/QSx29/

とはいえ、ラジオボタンはこれを行う最良の方法ではありません。意味的に正しくありません。グループ内で選択できるラジオ ボタンは 1 つだけです。代わりにチェックボックスを使用してみてください。マークアップをこれに変更してみてください:

<input type="checkbox" id="Check" />SelectAll
<br />
<input type="checkbox" />First
<br />
<input type="checkbox" />Second
<br />
<input type="checkbox" />Third
<br />
<input type="checkbox" />Fourth
<br />
<input type="checkbox" />Fifth</div>

JS コードを次のように置き換えます。

$("#Check").change(function () {
    $("input:checkbox").prop("checked", this.checked);
});

ここにデモがあります:http://jsfiddle.net/hungerpain/QSx29/1/

于 2013-06-22T11:15:46.220 に答える
1

あなたはこれが必要です

$("#Check").change(function () {
    $("input[type=radio]").prop("checked", this.checked);
});

デモ----> http://jsfiddle.net/kLnyD/

于 2013-06-22T11:16:17.667 に答える
0

これを試して

http://jsfiddle.net/4u9sQ/

 $("#Check").change(function () {
                        if ($(this).is(':checked')) {
                            $("input[type=radio]").prop("checked", true);

                        }
                        else { $("input[type=radio]").prop("checked", false); }
                    });
于 2013-06-22T11:21:54.913 に答える