jQuery“属性包含"选择器具有动态值 [英] jquery 'attribute contains' selector with a dynamic value
问题描述
假设我有一个变量questionId
,它是一个整数,我想查找tr
元素,这些元素的id中有片段("question_"+questionId
).我怎样才能做到这一点?我以为我可以使用jQuery 属性包含"选择器来做到这一点.
Let's say that i have a variable questionId
which is an integer, and i want to find tr
elements that have the fragment ("question_"+questionId
) in their id. How can i do this? I thought that i would be able to do it with the jquery 'attribute contains' selector.
例如,这适用于非动态值
Eg, this works, for a non-dynamic value,
$("tr[id*='quiz_question_7674']")
但是,我不知道如何在其中插入变量值.例如,这不起作用:
but, i can't work out how to plug the variable value in there. This doesn't work for example:
questionId = 7674;
$("tr[id*='quiz_question_'+questionId]")
有任何想法吗?有没有比属性包含"更好的方法呢?我有种明显的缺憾.
Any ideas anyone? Is there a better way than 'attribute contains' to do it? I have the feeling i'm missing something obvious.
谢谢,最大
编辑-已解决. h,我确实缺少明显的东西.我一直忘了它只是一个字符串,仅此而已:
EDIT - SOLVED. doh, i am indeed missing something obvious. I keep forgetting that it's just a string, nothing more:
$("tr [id * ='quiz_question _" + questionId +']")
$("tr[id*='quiz_question_"+questionId+"']")
推荐答案
您遇到错误:
var questionId = 7674;
$("tr[id*='quiz_question_" + questionId + "']");
注意:
- 请使用var声明变量.
- questionId是一个变量.它不是选择器的一部分.您应该将questionId连接到字符串.
这篇关于jQuery“属性包含"选择器具有动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!