简体   繁体   中英

how to concatenate a descendant selector to a variable in jquery

i am trying to put a variable in a jquery descendant selector and for some reason it does not work i think it might have to do with the fact that descendant selector needs to be together without any Apostrophe's in between but im not sure

<form>
    <div id= question>
<ul class="question-list">

  <LH id=que>gfh</LH>

here is the place i am trying to change

  <li id="choice-1">
    <input type="radio" name="radio" id="choice" data-price="100" value="product1" checked="checked">
    <label for="choice1"><span>question 1</span></label>
  </li>

  <li>
    <input type="radio" name="radio" id="choice" data-price="110" value="choice2"><label for="choice2"><span>question 2 [+ 10 $]</span></label>
  </li>

  <li>
    <input type="radio" name="radio" id="choice" data-price="110" value="product3"><label for="product3"><span>question 3 [+ 10 $]</span></label>
  </li>
</ul>
<input type="button" id="jqtest" value="next"  onclick= nextQuestion_onclick>
</div>
</form> 




var allQuestions = [{question: "Who is Prime Minister of the United Kingdom?", choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"], correctAnswer:0}];

this one works but when i change it into a

$('#choice-1 span').text(allQuestions[0].choices[0]);   

but when i change it into a concatenate it doesn't work

$(document).ready(function(){
var currentChoise= '1';

$('#que').text(allQuestions[0].question);
$('#choice-'+currentChoise+ 'span').text(allQuestions[0].choices[0]);

});

here is a jsfiddlelink http://jsfiddle.net/jpu1gchd/

在跨度之前应该有一点。

$('#choice-'+currentChoise+ ' span').text(allQuestions[0].choices[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM