Javascript 用于选择带有类的输入字段的 jQuery 选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6984926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery selector for selecting an input field with a class
提问by Kishore
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
我需要一个 jQuery 选择器,我可以在其中选择一个需要类的文本字段
right now to select textfield i use $(':input[type="text"]')
现在选择文本字段我使用 $(':input[type="text"]')
how do i also put a condition to select class="required."
我如何还设置一个条件来选择 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 tried
我需要一个可以选择所有 type="text", type="password" 的选择器,其中 class="required" 我试过了
$('#validate_info $("input.required").filter("[type="password"],[type="text"]")');
but this doesnt seem to work.
但这似乎不起作用。
回答by Rocket Hazmat
$(':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
将得到所有的表单元素(<select>
,<textarea>
等)
input
will only get <input>
elements, and is slightly faster.
input
只会获取<input>
元素,并且速度稍快。
回答by MikeM
$('.required:input[type="text"]')
(also $('input.required[type="text"]')
)
(也$('input.required[type="text"]')
)
回答by Joseph Marikle
$(':input[type="text"][class="required"]')
That should work
那应该工作
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
大声笑......我无法透过树木看到森林。:P 我也会留下我的,但只是为了说明可以通过多少种方法来完成。XD
回答by Kyle
If you just add the class to your selector, it should do what you are looking for.
如果您只是将类添加到选择器中,它应该可以满足您的要求。
$('.required:input[type="text"]')