Javascript jQuery ID 以
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5413841/
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 ID starts with
提问by DG3
I am trying to get all elements with an id starting with some value. Below is my jQuery code. I am trying to use a JavaScript variable when searching for items. But it does not work. What am I missing below? So the id 'value' am searching is the value of the clicked element
我正在尝试使用以某个值开头的 id 获取所有元素。下面是我的 jQuery 代码。我试图在搜索项目时使用 JavaScript 变量。但它不起作用。我在下面缺少什么?所以正在搜索的 id 'value' 是被点击元素的值
$(document).ready(function() {
$('input[name$="_chkmulti"]').click(function(){
var value = $(this).val();
$("td[id^= + value +]").each(function(){
alert("yes");
});
});
});
回答by Mark Kahn
try:
尝试:
$("td[id^=" + value + "]")
回答by ?ime Vidas
Here you go:
干得好:
$('td[id^="' + value +'"]')
so if the value is for instance 'foo'
, then the selector will be 'td[id^="foo"]'
.
因此,如果值为 example 'foo'
,则选择器将为'td[id^="foo"]'
.
Note that the quotes are mandatory: [id^="...."]
.
请注意,引号是强制性的:[id^="...."]
。
Source: http://api.jquery.com/attribute-starts-with-selector/