[英]Is there a concise jQuery selector for a list of specific input types (type='text', etc)?
有更简洁的方法吗?
// 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'])";
请注意,它还包括输入和文本区域,这也意味着默认情况下将包括将来向HTML规范添加的内容(对于那些认为不可能的人)。
当选择器不是字符串时, 委托()方法似乎无法正常工作。 要解决此问题,只需将逻辑放入委托处理程序中
//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.