jQuery: Get all text inputs where no type is defined

蓝咒 提交于 2019-12-25 11:58:57

问题


When i use $("input[type=text]") i absolutely mean all text boxes but it only return inputs that type has been set for them.

For example in:

<input name="text1" />
<input name="text2' type="text"/>

text1 is not included.

how to get all text inputs regardless type has been set or not ?


回答1:


Selects the ones with type=text, and the ones without type attribute.

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



回答2:


$('input:text')

From the official documentation:

As of jQuery 1.5.2, :text selects input elements that have no specified type attribute (in which case type="text" is implied).




回答3:


You can use the filter method:

$("input").filter(function(){
    return this.type === 'text'
})​

http://jsfiddle.net/hgzmQ/




回答4:


if all of the names of your inputs are contain 'text' in them (otherwise how would you understand that it's a text box?), you could try.

$('input[name*="text"]');



回答5:


If you don't set the type, then [type="text"] will preclude the ones without type.

if you select $('input') you will get every input regardless of type, so that could be a text field, or not. but it would still apply to every input field.

If you don't set the type, it won't be shown as type=text, but it will be rendered as such. That means you can't select JUST [type="text"], but you could always preclude others though as follows:

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

Not sure if this helps at all.



来源:https://stackoverflow.com/questions/12866903/jquery-get-all-text-inputs-where-no-type-is-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!