jQuery - 根据价值获得选择 [英] jQuery - Get a selection based on value
问题描述
我确信这很简单,但我似乎可以找到它。
I'm sure this is painfully simple but I just can seem to find it.
我需要从它们的值中选择一些文本框。我不需要价值,我需要元素。我想要这样的东西:
I need to get a selection of textboxes from their value. I don't need the value, I need the elements. I want something like:
$(".ProductCode [value:'hideme']").hide();
我最终以
unrecognized expression: [value:'hideme']
btw,
$(".ProductCode").each(function() { if ($(this).val() == 'hideme') $(this).hide(); });
工作正常,但看起来不太干净。
Is working but it doesn't seem very clean.
推荐答案
使用属性等于选择器 of jQuery
Use the attribute equals selector of jQuery
$(".ProductCode[value='hideme']").hide();
更准确地说,你也可以使用多个属性选择器:
To be more precise, you could also use the multiple attribute selector:
$("input[class='ProductCode'][value='hideme']").hide();
两者之间的区别在于第一个选择具有特定类和值的所有元素。第二个仅选择具有特定类别和值的所有INPUT。
The difference between the two is that the first selects all elements with a certain class and value. The second only selects all INPUTs with a certain class and value.
此选择器将选择适用元素的全部。这样 hide()
函数将隐藏所有元素。所以不需要用 each()
或其他东西手动遍历所选元素。 hide()
自动为你做这件事。
This selectors will select all of the applicable elements. So that hide()
function will hide all of the elements. So there is no need to "manually" iterate through the selected elements with each()
or other things.. hide()
automatically does that for you.
这篇关于jQuery - 根据价值获得选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!