如何使用JQuery

时间:2017-11-03 18:37:34

标签: javascript jquery html

我是JQuery的新手,我试图创建一个动态填充单选按钮并将它们存储在数组中的测验。我有几个问题。我有问题和单选按钮工作。所选的值不会返回到数组选择中#39;。我尝试了一些我在这里和其他地方找到的东西。它显示" on"的值。或"未定义"当我做出选择时,对于数组。由于这个我的警告"选择不起作用,它只是让你转到下一个问题。

这是我的代码。

function billQuestions() {
      const myBills = [
          { question: 'Who is on the One Dollar Bill?',
            choices: ['Thomas Jefferson', 'George Washington'],
            correctAnswer: 'George Washington' },
          { question: 'Who is on the Fifty Dollar Bill?',
            choices: ['George Washington', 'John Quincy Adams'],
            correctAnswer: 'John Quincy Adams' },
          ];


      let questionCounter = 0;     /* Tracks question number */
      let selections = [];         /* Array containing user choices */
      let quiz = $('#quiz');       /* Quiz div object */


/* Pulls the length of the Choices Objects */
     getChoicesLength();
function getChoicesLength () {
     let clength = Object.keys(myBills[1].choices).length;
return(clength);
}

/* If no user selection, progress is stopped */
      if(!(isNaN(selections[questionCounter]))) 
          {alert('Please make a selection!');
      } else {questionCounter++;
          displayNext();
        }
      });


/* Creates a list of the answer choices as radio inputs */
    function createRadios(index) {
        let radioList = $('<ul>');
        let input = $('');
           clength = getChoicesLength();

    for (let i = 0; i < clength; i++) {
        input = $('<li><input type="radio" name="answer" />' + 
                myBills[index].choices[i] + '</input></li>');
        radioList.append(input);
     }
  return radioList;
}

/* Reads the user selection and pushes the value to an array */
function choose() {
  selections[questionCounter] = $('input[name="answer"]:checked').val();
}

/* Displays next requested element */
function displayNext() {
  quiz.fadeOut(function() {
    $('#question').remove();

    clength = getChoicesLength();  

    if(questionCounter < clength){
      let nextQuestion = createQuestionElement(questionCounter);
      quiz.append(nextQuestion).fadeIn();
      if ((isNaN(selections[questionCounter]))) {
        $('input[value='+selections[questionCounter]+']').prop('checked', 
            true);
        console.log(selections);
        console.log(questionCounter);
      }

0 个答案:

没有答案