LoginSignup
6
6

More than 5 years have passed since last update.

How to get Checkbox value with jQuery 2.x

Last updated at Posted at 2014-05-19

なんか jQuery 2.x では仕様が変わったようで…(?)

チェックボックスの値を得るには .is(':checked') もしくは .prop('checked') を使います。

<!DOCTYPE html>
<input id="cb"
       type="checkbox"
       onclick="javascript:showCB()">
<!-- IE 非対応 -->
<script src='http://code.jquery.com/jquery-2.1.1.min.js'></script>
<script src='cb.js'></script>
cb.js
function showCB()
{
  alert($('#cb').is(':checked'));
}

smzkさんのご指摘で、こちらでも取得できることが分かりました。ありがとうございます。

cb.js
function showCB()
{
  alert($('#cb').prop('checked'));
}

以下を参考にしました。
http://stackoverflow.com/questions/18439468/why-is-jquery-is-not-checking-unchecking-checkbox

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