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

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

or

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

这样的事情存在吗?


当前回答

正如@livefree75所说:

jQuery 1.5.x及以下版本

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

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

但是在jQuery的新版本中,我们必须使用如下内容:

jQuery 1.6+

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

然后你可以这样做:

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

其他回答

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

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

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

选中和取消选中

$('.myCheckbox').prop('checked', true);
$('.myCheckbox').prop('checked', false);

要使用jQuery 1.6或更高版本选中复选框,只需执行以下操作:

checkbox.prop('checked', true);

要取消选中,请使用:

checkbox.prop('checked', false);

下面是我喜欢使用jQuery切换复选框的内容:

checkbox.prop('checked', !checkbox.prop('checked'));

如果使用jQuery 1.5或更低版本:

checkbox.attr('checked', true);

要取消选中,请使用:

checkbox.attr('checked', false);

如果您使用的是.prop('checked',true|false),并且没有更改复选框,则需要像这样添加触发器('click'):

// Check
$('#checkboxF1').prop( "checked", true).trigger('click');


// Uncheck
$('#checkboxF1').prop( "checked", false).trigger('click');

你可以的

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

or

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

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

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

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

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

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

$(".myCheckbox").each(function(){
    $("#mycheckbox").click()
});