6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQueryでチェックボックスを操作する

Last updated at Posted at 2014-07-26

スクリーンショット 2014-07-26 11.15.40.png

jQueryでチェックボックスを操作するには".attr"と"prop"を使う方法があります。

$(要素).attr("checked", true);
$(要素).prop("checked", true);

".attr"を使う方法だと二回目以降に動作しない現象が発生します。".prop"を使う方法だと正常に動作するのでjQueryでチェックボックスを操作するときはこちらを使うようにした方がいいみたいです。

サンプルコード

<apex:page >
  <apex:includeScript value="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" />
  <style>
    #chk div {
      margin: 5px 0;
    }
  </style>
  <div id="chk">
    <div>
      <input type="checkbox" id="inputCheck" onchange="return changeChecked(this);" />
      <label for="inputCheck">Check!!</label>
    </div>
    <div style="margin-top: 15px;" />
    <div>
      <input type="checkbox" id="outputAttrChecked" />
      <label for="outputAttrChecked">Attr</label>
    </div>
    <div>
      <input type="checkbox" id="outputPropChecked" />
      <label for="outputPropChecked">Prop</label>
    </div>
  </div>
  <script>
    var $j =jQuery.noConflict();
    function changeChecked(chk) {
      $j('#outputAttrChecked').attr("checked", chk.checked);
      $j('#outputPropChecked').prop("checked", chk.checked);
      return false;
    }
  </script>
</apex:page>
6
5
2

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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?