将HTML引入JS以广播图像

问题描述 投票:0回答:1

我正在做一些测验。在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  

   }}

当我看到网站时,我收到此错误消息。

未捕获的TypeError:无法在checkAnswers读取null的属性“value”

javascript html connection broadcast
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.