选择带条件的多个标签

时间:2012-05-02 08:08:53

标签: javascript jquery jquery-selectors

我想选择不是 readonly <的所有可编辑表单元素(input[type=text]input[type="radio"]input[type="checkbox"]selecttextarea) / em>或已禁用并拥有名称。是否可以使用单个选择器而不重复每种类型的只读和禁用属性?

换句话说,我可以提供[name]:not([disabled]):not([readonly])作为全局过滤器,而不是为每种输入类型重复吗?

我对单个选择器特别感兴趣,因为我想将它作为选择器过滤器提供给$.live / $.on。我不能使用.not或任何此类方法,因为可以动态添加元素。

2 个答案:

答案 0 :(得分:1)

$('input[type="text"], input[type="radio"], input[type="checkbox"], select, textarea')
     .is(':not([disabled]):not([readonly])');

我发现这个好的解决方案似乎有效。

$('(input[type="text"], input[type="radio"], input[type="checkbox"], select, textarea):not([disabled]):not([readonly])')

基本上,您似乎可以在选择周围添加(),然后执行:not()条件。上帝,我喜欢jQuery!

答案 1 :(得分:1)

您可以使用filter方法:

$("input[type='text']:enabled, textarea:enabled, select:enabled, input[type='radio']:enabled")
.filter(function(){ return $(this).attr("readonly", false); // or true
}).on("click", function(){
     .....
})