我想这样做,使用jQuery勾选复选框:
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
这样的事情存在吗?
我想这样做,使用jQuery勾选复选框:
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
这样的事情存在吗?
当前回答
2019年1月编辑
您可以使用:.prop(propertyName)-添加的版本:1.6
p{margin:20px 0 0;}b{颜色:红色;}标签{颜色:红色;}<!doctype html><html lang=“en”><head><meta charset=“utf-8”><script src=“https://code.jquery.com/jquery-1.10.2.js“></script></head><body><input id=“check1”type=“checkbox”checked=“checked”><label for=“check1”>检查此处</label><p></p><脚本>$(“input”).change(函数){var$input=$(this);$(“p”).html(“.attr(\”checked\“):<b>”+$input.attr“(”checked“)+”</b><br>“+“.prop(\”checked\“):<b>”+$input.prop(”checked“)+“</b><br>”+“.is(\”:checked \“):<b>”+$input.is(“:checked”)+“</b>”);}).change();</script></body></html>
论角度框架
示例1
在.html文件中
<input type="checkbox" (change)="toggleEditable($event)">
在.ts文件中
toggleEditable(event) {
if ( event.target.checked ) {
this.contentEditable = true;
}
}
示例2
在.html文件中
<input type="checkbox" [(ngModel)]="isChecked" (change)="checkAction(isChecked ? 'Action1':'Action2')" />
其他回答
$('controlCheckBox').click(function(){
var temp = $(this).prop('checked');
$('controlledCheckBoxes').prop('checked', temp);
});
试试看:
$('#checkboxid').get(0).checked = true; //For checking
$('#checkboxid').get(0).checked = false; //For unchecking
这可能会帮助某人。
HTML5
<input id="check_box" type="checkbox" onclick="handleOnClick()">
JavaScript。
function handleOnClick(){
if($("#check_box").prop('checked'))
{
console.log("current state: checked");
}
else
{
console.log("current state: unchecked");
}
}
对于jQuery 1.6+
$('.myCheckbox').prop('checked', true);
$('.myCheckbox').prop('checked', false);
对于jQuery 1.5.x及以下版本
$('.myCheckbox').attr('checked', true);
$('.myCheckbox').attr('checked', false);
为了检查,
$('.myCheckbox').removeAttr('checked');
如果您考虑使用vanilla js而不是jquery,那么有一个解决方案:
//for one element:
document.querySelector('.myCheckBox').checked = true /* or false */ //will select the first matched element
//for multiple elements:
for (const checkbox of document.querySelectorAll('.myCheckBox')) {
checkbox.checked = true //or false
}