如何在javascript中放置文本字段

时间:2015-08-30 09:07:35

标签: javascript

我想在下面的代码中的第一个单选按钮上添加文本字段和问题。 点击按钮后,它会转到下一个带有文字字段的问题。 它会如何运作?帮助我。

流程将保持与当前代码中的相同。但我想在第一个单选按钮文本上添加文本字段。

 <script>
var pos = 0, test, test_status, question, choice, choices, chA, chB, chC,       correct = 0;
var questions = [

[ "What is 10 + 4?", "12", "14", "16", "B" ],
[ "What is 20 - 9?", "7", "13", "11", "C" ],
[ "What is 7 x 3?", "21", "24", "25", "A" ],
[ "What is 8 / 2?", "10", "2", "4", "C" ]];

function _(x){
    return document.getElementById(x);
  }

function` renderQuestion(){
    test = _("test");
    if(pos >= questions.length){
        test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions` correct</h2>";
        _("test_status").innerHTML = "Test Completed";
        pos = 0;
        correct = 0;
        return false;
    }
    _("test_status").innerHTML = "Question "+(pos+1)+" of "+questions.length;
    question = questions[pos][0];
    chA = questions[pos][1];
    chB = questions[pos][2];
    chC = questions[pos][3];
    test.innerHTML = "<h3>"+question+"</h3>";
    test.innerHTML += "<input type='radio' name='choices' value='A'> "+chA+"<br>";

    test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
    test.innerHTML += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>";


    <!-- test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>"; -->
}

function checkAnswer(){
    choices = document.getElementsByName("choices");
    for(var i=0; i<choices.length; i++){
        if(choices[i].checked){
            choice = choices[i].value;
        }
    }
    if(choice == questions[pos][4]){
        correct++;
    }
    pos++;
    renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>

1 个答案:

答案 0 :(得分:0)

您可以在单选按钮的 onlclick 事件中使用focus()功能。您可能还希望将元素ID添加到单选按钮,以标识要关注的特定单选按钮或文本。

我没有足够的声誉来添加评论所以我必须在这里询问,如果你可以更具体地说明你想要做什么,因为文本字段指的是文本框而我看不到任何应用程序中的文本框用法。