是否有一个简洁的jQuery选择器用于特定输入类型的列表(type =' text'等)?

时间:2012-04-29 14:56:02

标签: jquery jquery-selectors textinput

有更简洁的方法吗?

// exclude all HTML 4 except text and password, but include HTML 5 except search
var inputSelector = "textarea,input[type='text'],input[type='password'],input[type='color']," +
    "input[type='date'],input[type='datetime'],input[type='datetime-local']," +
    "input[type='email'],input[type='month'],input[type='number'],input[type='range']," +
    "input[type='tel'],input[type='time'],input[type='url'],input[type='week']";

注意:我无法使用.Not()函数,因为此选择器将用于.delegate()函数。此外,最好是默认情况下我可以使选择器包含未来的输入类型(HTML 6>)。

答案:

这是我现在拥有的。比以前好多了。如果它可以更精致,请告诉我。

// exclude all HTML 4 except text and password, but include HTML 5 except search
var inputSelector = "textarea,input:not([type='checkbox'],[type='radio'],[type='button']," +
    "[type='image'],[type='submit'],[type='reset'],[type='file'],[type='search'])";

请注意,它还包括输入和textarea,这也意味着默认情况下将包含对HTML规范的未来添加(对于那些认为不可能的人)。

3 个答案:

答案 0 :(得分:1)

当选择器不是字符串时,似乎 delegate()方法效果不佳。要解决此问题,只需将您的逻辑放在委托处理程序

//Input types I do not want to select
var typeArray = ["text", "date"];

$("form").delegate("input", "click", function() {
    if($.inArray(this.type, typeArray) === -1) {
        //Put your logic here
    }
});

答案 1 :(得分:0)

MДΓΓБДLL和xFortyFourx都给了我这个解决方案,结果证明这是我寻求的最佳答案,但不确定为什么它在评论中回答而不是答案。

$("input:not([type='radio'], [type='text'])")

答案 2 :(得分:0)

如果您想过滤掉多种类型,可以执行以下操作:

$('input[type!="radio"][type!="text"]')