最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery 'attribute contains' selector with a dynamic value - Stack Overflow

programmeradmin7浏览0评论

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.

thanks, max

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+"']")

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.

thanks, max

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+"']")

Share Improve this question edited Mar 5, 2010 at 15:01 Max Williams asked Mar 5, 2010 at 14:56 Max WilliamsMax Williams 32.9k34 gold badges135 silver badges204 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 15

You have error:

var questionId = 7674;
$("tr[id*='quiz_question_" + questionId + "']");

Notes:

  1. Please use var to declare variables.
  2. questionId is a variable. It is not part of the selector. You should concatenate questionId to the string.

Yep, you almost had it:

var selector = "tr[id*='quiz_question_" + questionId + "']";
$(selector)
发布评论

评论列表(0)

  1. 暂无评论