我有一个页面上一堆隐藏的投入......按价值选择输入?
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
等...
每个输入可以有任意值。
在jquery中,检查这些输入之一是否已被设置为已知特定值的最佳方法是什么?
感谢
我有一个页面上一堆隐藏的投入......按价值选择输入?
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
等...
每个输入可以有任意值。
在jquery中,检查这些输入之一是否已被设置为已知特定值的最佳方法是什么?
感谢
如果你知道值属性被设置为一个特定的值,你可以使用属性选择
$('input[value="something"]');
http://api.jquery.com/category/selectors/
编辑补充:
你可能想链接attr选择器为input[name="thing"][value="something"]
和@Drackir是正确的,你可以测试它是否匹配是否一个或多个元素是通过匹配的length
属性设置的。
以下是我的建议。你想循环所有与名称的项目,并检查他们,并为每个匹配的做一些事情。
$('input[name="thing"]').each(function() {
var itemValue = this.val();
if(itemValue == "X" || itemValue == "Y")
alert("Item value is " + itemValue);
});
$('input[type="hidden"][value="5084405"]');
希望它有助于
具体而言,您可以用'如果($( '输入[值= “东西”]')。长度> 0){/ *做的东西* /} '**编辑**在@Kim Tranjan的回答中提到'[type = hidden]'时,值可能出现在其他输入元素中也是值得的。 – 2011-02-22 21:55:32