我想这样做,使用jQuery勾选复选框:

$(".myCheckBox").checked(true);

or

$(".myCheckBox").selected(true);

这样的事情存在吗?


当前回答

如jQuery文档所述,请注意Internet Explorer 9之前的Internet Explorer内存泄漏:

在版本9之前的Internet Explorer中,使用.prop()设置DOM元素属性设置为除简单基元值以外的任何值如果属性为在移除DOM元素之前未移除(使用.removeProp())从文档中删除。在没有内存的情况下安全地设置DOM对象的值泄漏,请使用.data()。

其他回答

这是完整的答案使用jQuery

我测试了它,它100%工作:D

    // when the button (select_unit_button) is clicked it returns all the checed checkboxes values 
    $("#select_unit_button").on("click", function(e){

             var arr = [];

             $(':checkbox:checked').each(function(i){
                 arr[i] = $(this).val(); // u can get id or anything else
             });

              //console.log(arr); // u can test it using this in google chrome
    });

这将选择具有指定属性且值包含给定子字符串“ckbItem”的元素:

$('input[name *= ckbItem]').prop('checked', true);

它将选择名称属性中包含ckbItem的所有元素。

你可以的

$('.myCheckbox').attr('checked',true) //Standards compliant

or

$("form #mycheckbox").attr('checked', true)

如果在onclick事件中有要激发的复选框的自定义代码,请改用此代码:

$("#mycheckbox").click();

可以通过完全删除该属性来取消选中:

$('.myCheckbox').removeAttr('checked')

您可以这样选中所有复选框:

$(".myCheckbox").each(function(){
    $("#mycheckbox").click()
});
$('controlCheckBox').click(function(){
    var temp = $(this).prop('checked');
    $('controlledCheckBoxes').prop('checked', temp);
});

您可以使用JavaScript以不同的方式检查复选框检查条件。你可以在下面看到。

第一种方法-$('.myCheckbox').prop('checked',true);第二种方法-$('.myCheckbox').attr('checked',true);第三种方法(如果选中或未选中复选框,则用于检查条件)-$('.myCheckbox').is(':checked')