LoginSignup
14
14

More than 5 years have passed since last update.

jQueryで複数選択不可のチェックボックスをつくる

Last updated at Posted at 2016-05-24

目的

  • デフォルトでは1つ目にチェック
  • 片方選んだら片方のチェックボックスは外れる
  • ラベルでもチェックはつけれる

コード

index.html
<input type="checkbox" class="checkbox" id="first" checked><label for="pre">1つ目</label>
<input type="checkbox" class="checkbox" id="second"><label for="deferred">2つ目</label>
common.js
$(".checkbox").on("click", function(){
        $('.checkbox').prop('checked', false);  //  全部のチェックを外す
        $(this).prop('checked', true);  //  押したやつだけチェックつける
});

日本語リファレンスによると、.prop()は

属性プロパティに値を設定、または設定されている値を取得します。

14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14