2016-11-29 62 views
0

我正在为某件事进行测验。在HTML中有一个用户可以输入他们的答案的文本框。我想把它发送给JS来播放一个图像(如果正确的话是'正确的'图像,如果错误的话是'错误'的图像)。这是迄今为止与这个问题有关的代码。将HTML引入JS以播放图像

的Html

<input class="respBox" id="ans1" placeholder="Insert value of X here"> 
<p id="response1"></p> 


<button type="button" class="specialButton" onclick="checkAnswers()">Submit</button> 

JS 功能checkAnswers(){

if (ans1 === "7.5") {// If x is 7.5 
    response1 = rightAns;//rightAns is image for right answer 
    document.getElementById('response1').innerHTML = response1;//updates scoring system 
    score++;//adds one point to the user's score 
} else { 
    response1 = wrongAns;//wrongAns is image for wrong answer 
    document.getElementById('response1').innerHTML = response1;//updates scoring system 

    }} 

我收到此错误信息,当我看到的网站。

遗漏的类型错误:在checkAnswers无法读取空 的特性“值”

回答

0
<form action="javascript:checkAnwsers()"> 
    <input class="respBox" name="answer" placeholder="Insert value of X here"> 
    <p id="response1"></p> 

    <input type="submit" value="submit"> 
</form> 

将一个表单元素和行动的内部输入字段将是函数来调用和使用属性名命名输入字段

用这个在JavaScript中访问数据:

var formElements = document.forms['myform'].elements['inputTypeName'].value;