不同元素类型的name属性的jQuery选择器 [英] jQuery selector of name attribute for different element types
问题描述
我有一个行表.在其中一列中,有些行的跨度为静态文本,有些行的select包含要选择的值.该列中的所有元素都具有相同的name属性.在表单提交中,我遍历行并希望获取所有列的值.我希望有一个jQuery选择器语句从该元素获取值(跨度或使用name属性为"materialValue"进行选择).我将如何使用jQuery?以下是html代码段.
<table>
<tr><td>
<span id="materialValue1" name="materialValue>ONE</span>
</td></tr>
<tr><td>
<span id="materialValue2" name="materialValue>TWO</span>
</td></tr>
<tr><td>
<select id="materialValue3" name="materialValue>
<option>ONE</option>
<option>TWO</option>
<option>THREE</option>
</select>
</td></tr>
<tr><td>
<select id="materialValue4" name="materialValue>
<option>ONE</option>
<option>TWO</option>
<option>THREE</option>
</select>
</td></tr>
</table>
我习惯于指定元素类型,然后指定带有属性名称/值的方括号.我不确定如何在没有元素类型名称的情况下指定jquery选择器.例如$('span[name="materialValue"]', this)
.指定$('[name="materialValue"]', this)
是否合法?对我来说看起来很奇怪.
您需要的只是一个属性选择器:
$("[name='MaterialValue']")
此外,您在html中的属性name
之后缺少右引号
请参阅此处以供参考: http://api.jquery.com/attribute-equals-selector/ >
I have a table of rows. In one of the columns, some rows have a span with static text, some rows have a select with values to choose from. All elements in that one column have the same name attribute. In my form submit, I iterate thru the rows and want to get the values for all columns. I would prefer to have one jQuery selector statement to get the value from that element (span or select with name attribute of "materialValue"). How would I do that with jQuery? Here follows the html snippet.
<table>
<tr><td>
<span id="materialValue1" name="materialValue>ONE</span>
</td></tr>
<tr><td>
<span id="materialValue2" name="materialValue>TWO</span>
</td></tr>
<tr><td>
<select id="materialValue3" name="materialValue>
<option>ONE</option>
<option>TWO</option>
<option>THREE</option>
</select>
</td></tr>
<tr><td>
<select id="materialValue4" name="materialValue>
<option>ONE</option>
<option>TWO</option>
<option>THREE</option>
</select>
</td></tr>
</table>
Edit: I'm used to specifying the element type then square brackets with the attribute name/value. I'm not sure how to specify the jquery selector without the element type name. e.g. $('span[name="materialValue"]', this)
. Is it legal to specify $('[name="materialValue"]', this)
? looks weird to me.
All you need is an attribute selector:
$("[name='MaterialValue']")
Also, you are missing closing quotes after your attribute name
in your html
Look here for reference: http://api.jquery.com/attribute-equals-selector/
这篇关于不同元素类型的name属性的jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!