问题
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