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

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

or

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

这样的事情存在吗?


当前回答

要选中复选框,应使用

 $('.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');
    }
});

其他回答

我们可以使用elementObject和jQuery来检查属性:

$(objectElement).attr('checked');

我们可以在所有jQuery版本中使用它,而不会出现任何错误。

更新:Jquery 1.6+具有替换attr的新prop方法,例如:

$(objectElement).prop('checked');

您还可以使用新方法扩展$.fn对象:

(function($)  {
   $.fn.extend({
      check : function()  {
         return this.filter(":radio, :checkbox").attr("checked", true);
      },
      uncheck : function()  {
         return this.filter(":radio, :checkbox").removeAttr("checked");
      }
   });
}(jQuery));

然后你可以这样做:

$(":checkbox").check();
$(":checkbox").uncheck();

或者,如果您使用其他使用这些名称的库,您可能希望为它们提供更多的唯一名称,如mycheck()和myuncheck()。

你可以试试这个:

$('input[name="activity[task_state]"]').val("specify the value you want to check ")

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

你可以这样做,如果你有身份证来检查它

document.getElementById('ElementId').checked=false

这个要取消选中

document.getElementById('ElementId').checked=真