Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jsp中多表单的javascript或jquery_Javascript_Jquery_Jsp - Fatal编程技术网

jsp中多表单的javascript或jquery

jsp中多表单的javascript或jquery,javascript,jquery,jsp,Javascript,Jquery,Jsp,我正在制作一个在线考试网站。我们有许多表单是由jsp中的为循环创建的。每张表格有1个选择题和4个答案 我使用jquery编写了一个脚本来检查用户在这个问题上的判断是对还是错 for (int i = 0; i < NumofQuestions; i++) { Question q = (Question) exam.get(i); %> <form> <p><b>Question <%=i+1%>

我正在制作一个在线考试网站。我们有许多表单是由jsp中的
循环创建的。每张表格有1个选择题和4个答案

我使用
jquery
编写了一个脚本来检查用户在这个问题上的判断是对还是错

for (int i = 0; i < NumofQuestions; i++) {
     Question q = (Question) exam.get(i); %>

     <form>
         <p><b>Question <%=i+1%>: </b> <%=q.getContent()%></p>
         <p><b>A. </b><input type="radio" name="answer" value="A"><%=q.getAnswerA()%><br></p>
         <p><b>B. </b><input type="radio" name="answer" value="B"><%=q.getAnswerB()%><br></p>
         <p><b>C. </b><input type="radio" name="answer" value="C"><%=q.getAnswerC()%><br></p>
         <p><b>D. </b><input type="radio" name="answer" value="D"><%=q.getAnswerD()%><br></p>
     </form>

     <span id="result"></span>

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
     <script type="text/javascript">
         $("form").on('click',function() {
             $this = $(this);
             var value = $this.find('input:radio[name=answer]:checked').val();
             var correct = "<%=q.getAnswer()%>";

             if (value == correct) {
                 $('#result').html('right');
             } else { 
                 $('#result').html('wrong');
             }
         });
     </script> 
<% } %>
for(int i=0;i
问题:

A.

B.

C.

D.

$(“表单”)。在('单击',函数()上{ $this=$(this); var value=$this.find('input:radio[name=answer]:checked').val(); var correct=“”; 如果(值==正确){ $('#result').html('right'); }否则{ $('#result').html('错误'); } });

虽然脚本包含在
for
循环中,但变量
correct
始终返回列表
exam
的最后一个元素。表示
q.getAnswer()
总是得到最后一个问题的答案,而不是正在做的问题。有什么方法可以解决这个问题吗?

如果您不介意,请将正确答案作为数据属性添加到表单中。否则,您需要向服务器发送ajax以获得答案。$(“表单”)将获取页面上的所有表单,并且您将为循环中的每个表单包含jQuery

或者,使用一个具有多个收音机的表格,其中每个收音机都有自己的名称


$(函数(){
$(“#formContainer”)。在(“单击”,“输入:收音机[name=answer]”上,函数(){
var value=this.value,//始终为选中值
$form=$(this),
correct=$form.data(“correct”);
$form.next().html(值==correct?'right':'error');
$(“#score”).html($(“.result:contains('right')).length);
});
});
对于(inti=0;i
问题:

A。

B。

C。

D。


最好显示从浏览器生成的html源代码,我的意思是,右键单击并查看源代码。谢谢,先生,它工作得很好。但是如果我想计算有多少正确的问题?我该怎么做,因为如果我声明变量“score”在jquery函数中,它会检查每个问题,而不是所有问题。我已经更新了答案。我使用一个容器来收集使用委托的任何收音机上的所有点击,并计算所有包含“正确”的结果。有没有办法将分数分配到一个变量中?因为当用户进行测试时,我会设置跨度“结果”和“分数”隐藏并在用户单击提交按钮时显示分数。p/s:我很抱歉,因为我要求您做很多事情,但我真的不知道,再次感谢您!
var score=$(“。结果:包含('right')).length;
您的解决方案很好,但这非常危险,因为用户可以通过右键单击查看页面源代码来查看所有答案。如果我想获得更高的安全性,我应该怎么做,先生?谢谢。