javascript 如何使用jQuery选择某些特定div内具有相同名称的所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21478461/
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 to select all elements with same name inside some specific div using jQuery
提问by bpbhat77
We have two divs: How to select all the input field as an array with name="divText" that are inside "div2".
我们有两个 div:如何选择所有输入字段作为“div2”内的 name="divText" 数组。
//Body of div1
//div1的主体
<div id="div1>
<input type="text" name="divText" value="v1" />
<input type="text" name="divText" value="v2"/>
<input type="text" name="divText" value="v3"/>
</div>
//Body of div2
//div2的主体
<div id="div2>
<input type="text" name="divText" value="q1" />
<input type="text" name="divText" value="q2"/>
<input type="text" name="divText" value="q3"/>
</div>
回答by Adil
Use Attribute Equals Selector [name="value"]along with ID Selector (“#id”). There is error in your html the closing quote of div id
div1
and div2
is missing as well.
使用Attribute Equals Selector [name="value"]和ID Selector (“#id”)。您的 html 中存在错误,即 div 的结束引号,id
div1
并且div2
也丢失了。
$('#div2 input[name="divText"]')
回答by Milind Anantwar
回答by Rune FS
Use the attribute selector
使用属性选择器
$("#div2 > input[name='divText']")
The jquery selector isn't an array but can for a lot of purposes be treated as such
jquery 选择器不是一个数组,但可以出于很多目的被这样对待
回答by Shree
Another way :
其他方式 :
$("#div2 input[type=text]")
回答by vlio20
As @adil answered it is the right way to do what you asked, but I think it is a better practice to put a class and then select it. For example:
正如@adil 回答的那样,这是完成您所要求的正确方法,但我认为放置一个类然后选择它是更好的做法。例如:
<div class="some_class">...</div>
<span class="some_class">...</span>
$('.some_class')
this what you are not constrained for any tag type.
这是您不受任何标签类型限制的内容。