我想这样做,使用jQuery勾选复选框:
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
这样的事情存在吗?
我想这样做,使用jQuery勾选复选框:
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
这样的事情存在吗?
当前回答
$("#mycheckbox")[0].checked = true;
$("#mycheckbox").attr('checked', true);
$("#mycheckbox").click();
最后一个将激发复选框的单击事件,其他的则不会。因此,如果您在onclick事件中为要激发的复选框设置了自定义代码,请使用最后一个。
其他回答
我们可以使用elementObject和jQuery来检查属性:
$(objectElement).attr('checked');
我们可以在所有jQuery版本中使用它,而不会出现任何错误。
更新:Jquery 1.6+具有替换attr的新prop方法,例如:
$(objectElement).prop('checked');
如果您使用ASP.NET MVC,生成许多复选框,然后必须使用JavaScript选择/取消选择所有选项,您可以执行以下操作。
HTML
@foreach (var item in Model)
{
@Html.CheckBox(string.Format("ProductId_{0}", @item.Id), @item.IsSelected)
}
JavaScript
function SelectAll() {
$('input[id^="ProductId_"]').each(function () {
$(this).prop('checked', true);
});
}
function UnselectAll() {
$('input[id^="ProductId_"]').each(function () {
$(this).prop('checked', false);
});
}
你可以的
$('.myCheckbox').attr('checked',true) //Standards compliant
or
$("form #mycheckbox").attr('checked', true)
如果在onclick事件中有要激发的复选框的自定义代码,请改用此代码:
$("#mycheckbox").click();
可以通过完全删除该属性来取消选中:
$('.myCheckbox').removeAttr('checked')
您可以这样选中所有复选框:
$(".myCheckbox").each(function(){
$("#mycheckbox").click()
});
if($('jquery_selector').is(“:checked”)){//某些代码}<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script>
$('controlCheckBox').click(function(){
var temp = $(this).prop('checked');
$('controlledCheckBoxes').prop('checked', temp);
});