多选择器jquery [英] multiple selectors jquery

查看:113
本文介绍了多选择器jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这:

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

将取消选中所有其他复选框,并选中人员类别和名称toddS。

will select a checkbox with the class of people and the name toddS while unchecking all of the other boxes.

如何为筛选器添加其他名称?我试过几种不同的组合,没有什么工作。这是我有:

How do I add another name to the filter? I've tried several different combinations and nothing works. This is what I have:

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


推荐答案

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

You can pass a function into .attr(), like this:

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

如果您需要稍后添加,可以使用适用于更多值的内容,例如< a href =http://api.jquery.com/jQuery.inArray/ =nofollow noreferrer> $。inArray() ,如这个:

If you need to add more later, you can use something that works for more values, for instance $.inArray(), like this:

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

这篇关于多选择器jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆