一个接一个地调用一个脚本

时间:2017-06-30 05:25:55

标签: javascript html node.js submit addeventlistener

我试图在我的代码所在的同一页面上使用两个java脚本:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>       
    <script src="socket.js"></script>
</head>
<body>    
  <div id="questionbox" width="100%"><%=data%></div>
</body>
</html>
<script>
  var socket = io.connect('http://localhost:3000');    

  socket.emit('questionreq',{res:'send me question'});
  questionbox = document.getElementById('questionbox');

  socket.on('question',function(res){
    question = res.question;
    optA = res.A;
    optB = res.B;
    optC = res.C;
    optD = res.D;
    answer = res.ans;

    questionbox.innerHTML += '<br>' + question + '<br>A:<input type="radio" name="question" id="A" value="A">' + optA + '<br>B <input type="radio" id="B" name="question" value="B">' + optB + '<br>'+ 'C :<input type="radio" name="question" id="C" value="C">' + optC + '<br>'+'D :<input type="radio" name="question" id="D" value="D">' + optD + '<br><input type="submit" id="submit" value="submit">'
  });    
</script>
<script>                                          
   btnSubmit=document.getElementById("submit");                                          
   btnSubmit.addEventListener('click', function() {                                          
     if (document.getElementById("A").checked)
       useranswer="A";
     else if (document.getElementById("B").checked)
       useranswer="B";
     else if (document.getElementById("C").checked)
       useranswer="C";
     else
       useranswer="D";
     if (1) {
       alert('correct!!!');
       socket.emit('correct',{res:'I am correct'});
     }
   });
</script>

这里我正在使用node.js,因此第一个脚本是向服务器请求问题并将其呈现给html页面。当用户点击提交按钮时,第二个脚本会发出提示,但是第二个脚本没有被调用,请告诉我这个脚本有什么问题。

1 个答案:

答案 0 :(得分:0)

你真的不需要两个单独的脚本标签。将第二个脚本保存在第一个脚本的socket.emit中。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>


     <script src="socket.js"></script>



</head>
<body>

<div id="questionbox" width="100%"><%=data%></div>

</body>
</html>
<script>
        var socket=io.connect('http://localhost:3000');

          socket.emit('questionreq',{res:'send me question'});
          questionbox=document.getElementById('questionbox');

        socket.on('question',function(res){
         question=res.question;
         optA=res.A;
         optB=res.B;
         optC=res.C;
         optD=res.D;
         answer=res.ans;
         
         questionbox.innerHTML+='<br>'+question+'<br>A:<input type="radio" name="question" id="A" value="A">'+optA+'<br>B <input type="radio" id="B" name="question" value="B">'+optB+'<br>'+'C :<input type="radio" name="question" id="C" value="C">'+optC+'<br>'+'D :<input type="radio" name="question" id="D" value="D">'+optD+'<br><input type="submit" id="submit" value="submit">';
var btnSubmit=document.getElementById("submit"); 

btnSubmit.addEventListener('click',function(){
                                      if(document.getElementById("A").checked)
                                        useranswer="A";
                                        else if(document.getElementById("B").checked)
                                        useranswer="B";
                                        else if(document.getElementById("C").checked)
                                        useranswer="C";
                                        else
                                         useranswer="D";
                                       if(1){
                                           alert('correct!!!');
                                          socket.emit('correct',{res:'I am correct'});
                                       }
                                       
                                      });
           });
                                     
</script>