jQuery :checkboxでチェックボックスを指定するサンプル

環境
jquery3.6.3
Google Chrome 111.0.5563.65(Official Build)

書式
$(“:checkbox").prop(“checked", true);
:checkbox チェックボックス
propメソッドを使ってtrueを指定してチェックが入ります

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
//ボタンの動作
$("#chk").click(function(){
// チェックボックス(属性)を指定
$(":checkbox").prop("checked", true);
});
});
</script>
</head>
<body>
<input type="checkbox" value="1" />
<button id="chk">確認</button>
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script> <script> $(document).ready(function(){ //ボタンの動作 $("#chk").click(function(){ // チェックボックス(属性)を指定 $(":checkbox").prop("checked", true); }); }); </script> </head> <body> <input type="checkbox" value="1" /> <button id="chk">確認</button> </body> </html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
  //ボタンの動作
  $("#chk").click(function(){
          // チェックボックス(属性)を指定
          $(":checkbox").prop("checked", true);
    });
});
</script>
</head>
<body>
<input type="checkbox"  value="1" />
<button id="chk">確認</button>
</body>
</html>

実行結果
「確認」ボタンを押すと、チェックボックスにチェックを入れます。

jQuery

Posted by arkgame