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

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

or

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

这样的事情存在吗?


当前回答

我无法通过以下方式实现:

$("#cb").prop('checked', 'true');
$("#cb").prop('checked', 'false');

true和false都会选中该复选框。对我有用的是:

$("#cb").prop('checked', 'true'); // For checking
$("#cb").prop('checked', '');     // For unchecking

其他回答

要选中复选框,应使用

 $('.myCheckbox').attr('checked',true);

or

 $('.myCheckbox').attr('checked','checked');

要取消选中复选框,应始终将其设置为false:

 $('.myCheckbox').attr('checked',false);

如果你这样做

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

它将删除所有属性,因此您将无法重置表单。

BAD DEMO jQuery 1.6。我想这是坏的。对于1.6,我将发布一个新的帖子。

新的工作演示jQuery1.5.2在Chrome中运行。

两个演示都使用

$('#tc').click(function() {
    if ( $('#myCheckbox').attr('checked')) {
        $('#myCheckbox').attr('checked', false);
    } else {
        $('#myCheckbox').attr('checked', 'checked');
    }
});

如果使用移动设备,并且您希望界面更新并将复选框显示为未选中,请使用以下方法:

$("#checkbox1").prop('checked', false).checkboxradio("refresh");
$('controlCheckBox').click(function(){
    var temp = $(this).prop('checked');
    $('controlledCheckBoxes').prop('checked', temp);
});

我错过了解决方案。我将始终使用:

if ($('#myCheckBox:checked').val() !== undefined)
{
    //Checked
}
else
{
    //Not checked
}

这是完整的答案使用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
    });