jQuery 如何按值选择隐藏字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1065981/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:33:34  来源:igfitidea点击:

How can I select a hidden field by value?

jqueryjquery-selectors

提问by flesh

I have the following HTML generated by an ASP.NET repeater:

我有以下由 ASP.NET 中继器生成的 HTML:

<table>
  <tr>
    <td><input type="hidden" name="ItemId" id="ItemId" value="3" /></td>
    <td>Terry</td>
    <td>Deleted</td>
    <td>Low</td>
    <td>Jun 21</td> 
  </tr>
  <!-- rows repeat -->
</table>

How do I select a particular hidden field by value, so that I can then manipulate the columns next to it?

如何按值选择特定的隐藏字段,以便我可以操作它旁边的列?

回答by Michael Bray

Using jQuery Selectors, you can target your element by a certain attribute matching the desired value:

使用jQuery Selectors,您可以通过匹配所需值的特定属性来定位您的元素:

$('input[value="Whatever"]');

This way you are targeting an inputelement, by the attribute valuethat is equal to the desired value.

通过这种方式,您可以input通过value等于所需值的属性来定位元素。

EDIT 5/14/2013:According to an answer below, this no longer works as of jQuery 1.9.

编辑 5/14/2013:根据下面的答案,从 jQuery 1.9 开始,这不再有效。

回答by Glyn Jones

Note: Since jQuery 1.9 the input[value="banana"] selector is no longer valid, because 'value' of the input is technically not an attribute. You need to use the (far more difficult to read) .filter

注意:从 jQuery 1.9 开始, input[value="banana"] 选择器不再有效,因为输入的 'value' 在技术上不是一个属性。您需要使用(更难阅读) .filter

E.g.

例如

$("input").filter(function () {
    return this.value === "banana";
});

See also: jQuery 1.9.1 property selector

另请参阅:jQuery 1.9.1 属性选择器

回答by Frenchi In LA

$('input:hidden[value=\'3\']');