试图在表单中显示单选按钮的内容

时间:2014-06-29 18:38:52

标签: javascript jquery

我正在使用jQuery创建一个测验。 我已经在二维数组中建立了测验问题和答案。 我有一个单独的数组中显示的答案选项(1-5)。 我已将答案选项存储到临时变量中,并希望将它们显示在单选按钮旁边。 答案选择(在我的代码中记为answerA = answerChoices [j] [0]等)正在正确输入,但它们没有显示。当我检查Chrome中的元素时,我发现它们正被引入。但是它们没有显示在#quizQuestion中的表单元素旁边。

我确信我正在做一些愚蠢但无法识别的事情。你可以帮忙吗?

这是html:

<!--practice quiz-->
<div id="practiceQuestions">

<div class="questionBox">

  <div class="theQuestion">
  <span id="quizQuestion"><img src="images/Practice_Tips_Math.png"></span>
  <INPUT type="submit" value="Start quiz now" id="startQuiz" class="viewable">

    <!--answer key beginsb-->

    <div class="answerBox">
      <form name="answerForm">
        <input type="radio" value="a" id="answerA" name="quizAnswer"><br />
        <input type="radio" value="b" id="answerB" name="quizAnswer"><br />
        <input type="radio" value="c" id="answerC" name="quizAnswer"><br />
        <input type="radio" value="d" id="answerD" name="quizAnswer"><br />
        <input type="radio" value="e" id="answerE" name="quizAnswer"><br /> 
      </form></div>
      <!--answer key end-->

  </div>

 </div> 



</div>
<!--practice quiz ends-->

这是jQuery:

var questionAnswer=[
['How many moons does Earth have?',1],
['How many moons does Saturn have?',31],
 ['How many moons does Venus have?',0]
];

var answerChoices=[
[1,2,3,4,5],
[18,32,15,1,9],
[3,5,1,0,9]
]

function askQuestion(j){
    //create radio button answer choices a-e
    //var radioBtn =$('<input type="radio" name="rbtnCount" value="a"  id="q1a"/><label for="q1a">'+question[0,1]+'</label><br/>');
    //display questions
    //console.log(radioBtn);
var answerA=answerChoices[j][0];
var answerB=answerChoices[j][1];
var answerC=answerChoices[j][2];
var answerD=answerChoices[j][3];
var answerE=answerChoices[j][4];

    if($('#startQuiz').is(':visible')) {
        $('#startQuiz').toggle();
        }
    if($('.answerBox').is(':hidden')) {
        $('.answerBox').toggle();
        $('.answerBox').css('margin-top','20px');

        }
        // display the question
        $(".theQuestion").css('margin-top','75px');
        $("#quizQuestion").text(questionAnswer[j][0]);
        //display the answers
        $('form #answerA').text(answerA);
        $('form #answerB').text(answerB);
        $('form #answerC').text(answerC);
        $('form #answerD').text(answerD);
        $('form #answerE').text(answerE);

    };

$(document).ready(function(){

    $('#startQuiz').on('click',function(event){
        event.preventDefault();

        for (var i=0; i<=questionAnswer.length; i++)
            {
                askQuestion(i);
            }
        }); 

});

1 个答案:

答案 0 :(得分:1)

您无法将text()设置为单选按钮,因此您必须在单选按钮旁边添加某种标签:

更新

<input type="radio" value="a" id="answerA" name="quizAnswer">

<input type="radio" value="a" id="answerA" name="quizAnswer">
<label for="answerA"></label>

并更新你的jQuery:

$('form #answerA').text(answerA);

为:

$('form label[for="answerA"]').text(answerA);

JSFiddle示例

<强> http://jsfiddle.net/moogs/RWk3E/1/