简体   繁体   中英

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. Also, it would be best if I could make the selector include future input types (HTML 6 >) by default.

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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