javascript jQuery 值选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4974033/
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
jQuery value selector
提问by Phillip Senn
I have:
我有:
<button class="Delete" value="1">Delete</button>
<button class="Delete" value="2">Delete</button>
<button class="Delete" value="3">Delete</button>
Given variable X that contains a value (in this case either a 1, a 2 or a 3), then how do I hide the button corresponding to the value in X?
给定包含值的变量 X(在本例中为 1、2 或 3),那么如何隐藏与 X 中的值对应的按钮?
I want to say something like:
我想说:
$('button').val(x).hide();
Meaning: "The button whose value is x, hide".
含义:“值为 x 的按钮,隐藏”。
回答by ?ime Vidas
$('button[value="' + x + '"]').hide();
回答by jAndy
You can write your own custom selector (I just felt someone should mention it). Could look like:
您可以编写自己的自定义选择器(我只是觉得有人应该提到它)。可能看起来像:
(function($) {
$.extend($.expr[':'], {
val: function(elem, i, attr) {
return elem.value === attr[3];
}
});
}(jQuery));
$('button:val(2)').hide();
回答by Blender
You'd do that inside of the actual selector:
您可以在实际选择器中执行此操作:
$('button[value="foo"]').hide();
回答by hollystyles
This would require concatenating x into an xpath, but i can't think of another way right now.
这需要将 x 连接到 xpath 中,但我现在想不出其他方法。
$(button[value='1']).hide();
EDIT: removed @, apparently deprecated, much like myself :)
编辑:已删除@,显然已弃用,很像我自己:)
回答by Roko C. Buljan
You don't even have to manually assign values to buttons:
您甚至不必手动为按钮分配值:
var i = 0; $('.Delete').attr('value', function() {i++; return ''+i;});
will do it fou you. Now you have your buttons values counted. But nevermind, (you don't even need values!) You can hide 'THIS clicked button' by doing:
你会做到的。现在您已经计算了您的按钮值。但是没关系,(您甚至不需要值!)您可以通过执行以下操作来隐藏“这个点击的按钮”:
$(".Delete").click(function() {
$(this).html("DELETED!").delay(900).slideUp(600).hide();
});
I've added a bit of animation. Or combine both! CIAO :)
我添加了一些动画。或者两者结合!再见 :)

