多个选择器jquery

这个:

$('input.people').attr('checked', false).filter('[name=toddS]').attr('checked', true); 

在取消选中所有其他框时,将选中一个包含人类和名称toddS的复选框。

如何在filter中添加其他名称? 我尝试了几种不同的组合,没有任何作用。 这就是我所拥有的:

 $('input.people').attr('checked', false).filter('[name=toddS], [name=gwenD]').attr('checked', true); 

您可以将函数传递给.attr() ,如下所示:

 $('input.people').attr('checked', function() { return this.name == 'toddS' || this.name == 'gwenD'; }); 

如果以后需要添加更多内容,可以使用适用于更多值的内容,例如$.inArray() ,如下所示:

 $('input.people').attr('checked', function() { return $.inArray(this.name, ['toddS', 'gwenD']) != -1; }); 

您可以在那里使用一个function来选择您想要的function 。

 $('input.people') .removeAttr('checked') .filter(function() { return ($(this).attr('name') === 'toddS' || $(this).attr('name') === 'gwenD'); }) .attr('checked', 'checked'); 

我也使用removeAttr()而不是将其设置为false 。 我还没有通过将jQuery设置为false来检查jQuery的function,但是从HTML的角度来看,未经检查应该意味着缺少该属性。