jQuery 如何根据类名获取给定 div 中的所有文本框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17301486/
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
How do I get all text boxes within a given div based on its class name?
提问by thebdawk05
This is really slowing down my progress this morning. Any help would be incredible. Here's my code that I'm using to try to get each text box:
这真的拖慢了我今天早上的进度。任何帮助都是不可思议的。这是我用来尝试获取每个文本框的代码:
$('.contactInfo input[type="text"]').each(function () {
});
The above code works but it keeps grabbing select boxes as well. I've tried using a colon in between the class name and input type identifier as well as a number of other ways to get each text box (:input, :text, type=text, etc.). Forgive my styling on here as I'm completely new to asking questions and haven't gotten the hang of the formatting.
Note: .contactInfo
is a class assigned to the div that contains the text boxes I want to get. Also, I'm using the chosen jquery plugin to style my select boxes as well. This may or may not have anything to do with my issue, just wanted to let you all know.
Thanks in advance for anything you guys can help me with!
Update: I've tried every solution listed here. Each one grabs the select box, however, it doesn't return its id. So, I'm just using a workaround that checks if its id is null so that I can skip it during the iterations. I really appreciate each of you for your input! Thanks!
上面的代码有效,但它也一直在抓取选择框。我已经尝试在类名和输入类型标识符之间使用冒号,以及许多其他方法来获取每个文本框(:input、:text、type=text 等)。请原谅我在这里的样式,因为我是全新的提问者并且还没有掌握格式。
注意:.contactInfo
是分配给包含我想要获取的文本框的 div 的类。此外,我还使用所选的 jquery 插件来设置选择框的样式。这可能与我的问题有关,也可能无关,只是想让大家知道。
在此先感谢你们能帮助我的任何事情!
更新:我已经尝试了此处列出的所有解决方案。每个人都抓住了选择框,但是,它不返回其 id。所以,我只是使用一种解决方法来检查它的 id 是否为空,以便我可以在迭代过程中跳过它。我真的很感谢你们每一个人的投入!谢谢!
回答by nekaab
See http://api.jquery.com/text-selector/:
请参阅http://api.jquery.com/text-selector/:
$('.contactInfo input:text')
$('.contactInfo input:text')
回答by Learner
$(".contactInfo").find("input[type=text]")
This should find all the textboxes within that div (.contactInfo). Another thing to note is that find() method is going to be much faster than context selectors.
这应该找到该 div (.contactInfo) 中的所有文本框。另一件需要注意的事情是 find() 方法将比上下文选择器快得多。
回答by reggaemahn
try this
尝试这个
$(".contactInfo").children("input[type=text]").each(function(){
//Do stuff here
});