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

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

or

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

这样的事情存在吗?


当前回答

假设问题是。。。

如何选中按值设置的复选框?

请记住,在典型的复选框集中,所有输入标记都具有相同的名称,它们的不同之处在于属性值:集合中的每个输入都没有ID。

Xian的答案可以用更具体的选择器扩展,使用以下代码行:

$("input.myclass[name='myname'][value='the_value']").prop("checked", true);

其他回答

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

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

这个要取消选中

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

当您选中以下复选框时:;

$('.className').attr('checked', 'checked')

这可能还不够。您还应调用以下函数:;

$('.className').prop('checked', 'true')

尤其是在删除复选框选中属性时。

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

$("#checkbox1").prop('checked', false).checkboxradio("refresh");

整体而言:

$("#checkAll").click(function(){
    $(".somecheckBoxes").prop('checked',$(this).prop('checked')?true:false);
});

正如@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();