Skip to content

Instantly share code, notes, and snippets.

@froop
Last active December 16, 2015 20:39
Show Gist options
  • Save froop/5493920 to your computer and use it in GitHub Desktop.
Save froop/5493920 to your computer and use it in GitHub Desktop.
[jQuery] チェックボックスへの attr("checked", x) が1回目しか効かなくて数時間悩んだ。なぜか <!DOCTYPE html> を消すと直る。 結局、attr() を prop() に変えたら問題なくなった。 val([x]) でも OK。
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<div>
<input name="check1" type="checkbox" value="on">
<button id="check-on">on</button>
<button id="check-off">off</button>
</div>
<script src="jquery.js"></script>
<script>
$("#check-on").on("click", function () {
$("input[name=check1]").attr("checked", true);
})
$("#check-off").on("click", function () {
$("input[name=check1]").removeAttr("checked");
})
</script>
</body>
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<div>
<input name="check1" type="checkbox" value="on">
<button id="check-on">on</button>
<button id="check-off">off</button>
</div>
<script src="jquery.js"></script>
<script>
$("#check-on").on("click", function () {
$("input[name=check1]").prop("checked", true);
// $("input[name=check1]").val(["on"]);
// $("input[name=check1]").get(0).checked = true;
})
$("#check-off").on("click", function () {
$("input[name=check1]").prop("checked", false);
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment