开发者

jQuery selector for selecting an input field with a class

I have

<input type="text" class="required" value="XXX" name="abc">
<input type="hidden" class="required" value="XXX" name="abcd">
<input type="text" class="required" value="XXX" name="abcef">

i have many input fields like this.

i need a jQuery selector where i can select a text field whose class is required

right now to select textfield i use $(':input[type="text"]')

how do i also put a condition to select class="required."

EDIT

so say now i have the following input fields

<div id="validate_info">
<input type="text" class="required" value="XXX" name="abc">
<input type="hidden" class="required" value="XXX" name="abcd">
<input type="text" class="required" value="XXX" name="abcef">
<input type="password" class="required" value="XXX" name="abcefg">
<input type="text"  value="XXX" name="abcefgsa">
<input type="password" value="XXX" name="abcsaefg">
</div>

i need one selector that can select all type="text", type="password" which has class="required" i t开发者_C百科ried

$('#validate_info $("input.required").filter("[type="password"],[type="text"]")');

but this doesnt seem to work.


$(':input.required[type="text"]')

EDIT:

This will be slightly faster:

$('input.required[type="text"]')

Note:

:input will get all form elements (<select>, <textarea>, etc.)

input will only get <input> elements, and is slightly faster.


$('.required:input[type="text"]')

(also $('input.required[type="text"]'))


$(':input[type="text"][class="required"]')

That should work

http://jsfiddle.net/ck8wt/

Edit

lol ... I couldn't see the forest through the trees. :P I'll leave mine up too though just as an illustration of how many ways this can be done. XD


If you just add the class to your selector, it should do what you are looking for.

$('.required:input[type="text"]')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜