jQuery 按值选择按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6552989/
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
Select button by value
提问by santa
How do I select a button with specific value with jQuery?
如何使用 jQuery 选择具有特定值的按钮?
<input type="button" value="My task">
回答by Niklas
You can use the attr=value
selector:
您可以使用attr=value
选择器:
$('input[type="button"][value="My task"]')
example: http://jsfiddle.net/niklasvh/yAUkC/
回答by pimvdb
Like:
喜欢:
$('input[type=button][value=My task]');
You want to select by attributes: jQuery page.
您要按属性选择:jQuery page。
So it says: select input
s of type button
with value My task
.
所以它说: select input
s of type button
with value My task
。
回答by prgmrDev
Just another approach, useful in some exceptional cases where value and text inside <button>
element is different
只是另一种方法,在<button>
元素内的值和文本不同的某些特殊情况下很有用
<button value="Value X">Value Y</button>
$("button:contains(Value Y)")
回答by Ruth Young
If prgmrDevs solution doesn't work for you this worked for me!
如果 prgmrDevs 解决方案对您不起作用,这对我有用!
$(':button[value="Value X"]')
$(':button[value="Value X"]')
回答by Roko C. Buljan
The suggestions here are great!
I'd like just to show you what you can also do:
let's say you have this:
这里的建议很棒!
我只想向您展示您还可以做什么:假设您有这个:
<input type="button" value="My task">
<input type="button" value="Your task">
<input type="button" value="Our job">
<input type="button" value="Their job">
and you want to get the values that have the word 'task'you can do it like:
并且您想获得带有“任务”一词的值,您可以这样做:
$('input[type=button][value~=task]')