简体   繁体   English

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

[英]Is there a concise jQuery selector for a list of specific input types (type='text', etc)?

Is there a more concise way to do this? 有更简洁的方法吗?

// 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']";

Note: I cannot use the .Not() function because this selector will be used in the .delegate() function. 注意:我不能使用.Not()函数,因为此选择器将在.delegate()函数中使用。 Also, it would be best if I could make the selector include future input types (HTML 6 >) by default. 另外,最好让选择器默认包含将来的输入类型(HTML 6>)。

Answer: 回答:

Here is what I have now. 这就是我现在所拥有的。 Much better than before. 比以前好多了。 If it can be refined more please let me know. 如果可以进一步完善,请告诉我。

// 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'])";

Note that it also includes input and textarea, which also mean future additions to the HTML spec will be included by default (to those who didn't think this was possible). 请注意,它还包括输入和文本区域,这也意味着默认情况下将包括将来向HTML规范添加的内容(对于那些认为不可能的人)。

It seems like the delegate() method does not work well when the selector is not a string. 当选择器不是字符串时, 委托()方法似乎无法正常工作。 To get around this issue, just put your logic within your delegate handler 要解决此问题,只需将逻辑放入委托处理程序中

//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
    }
});

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

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

如果要过滤出多种类型,可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM