关于两个变量比较的javascript验证

时间:2017-07-23 15:02:51

标签: javascript java jquery jsp

<%@ page import="java.sql.*" %>
<%@ page import="beans.Questionadd" %>

<%
int countanswer=0;
String counter = request.getParameter("counter");
String counter2 = request.getParameter("counter2");
System.out.println("counter  =  "+counter+", counter2 = "+counter2);

int count = 0, count2 = 0;

if(counter!=null)
{
    count =  Integer.parseInt(counter);
    count ++;
}
else
{
    count = 0;
}
System.out.println("count  =  "+count);
Questionadd[] questionadddarray = (Questionadd[])session.getAttribute("questionadddarray");
Questionadd questionadd = questionadddarray[count];

//System.out.println("questionadd.getQuestion()  =  "+questionadd.getQuestion());
String question = questionadd.getQuestion();
String qtype = questionadd.getQuestiontype();
String option1 = questionadd.getOption1();
String option2 = questionadd.getOption2();
String option3 = questionadd.getOption3();
String option4 = questionadd.getOption4();
String answer = questionadd.getAnswer();

%>
<html>
<head>
<script type="text/javascript">

//比较答案与所选选项和入会时的函数clickform // count2 value

function clickform()
{   
    var form = document.getElementById("form-id");

    alert("INSIDE CLICK");


var x = document.myform.option.value;

<%
answer=answer.trim();

%>

var answer = "<%=answer%>";
if(x==answer)
{  
    <% 
        if(counter2 != null)
        {
        count2 = Integer.parseInt(counter2);
        count2 ++;
        }
        else
        {
            count2 = 1;
        }

    %>
}
else
    {
    <% 
        if(counter2 != null)
        {
            count2 = Integer.parseInt(counter2);
        }
        else
        {
            count2 = 0;
        }
    %>
    }

document.getElementById("counter2").value=count2;  

<% System.out.println("count2 "+count2);%>

}
</script>

<html>
<head>
</head>
<body>
<form name="myform" id="myform" action="question1.jsp" method="post">
<table border="1">
<tr>
<td><%=question %></td>
</tr>
<tr>
<td><label><input type="radio" name="option" value="<%=option1 %>" required><%=option1%></input></label></td>
<td><label><input type="radio" name="option" value="<%=option2%>"><%=option2%></input></label></td>
</tr>
<tr>
<td><label><input type="radio" name="option" value="<%=option3%>"><%=option3%></input></label></td>
<td><label><input type="radio" name="option" value="<%=option4%>"><%=option4%></label></td>
</tr>
<tr>
<td><div style="text-align:center"><button id="form-id" onclick="return clickform();" />NEXT</button></div></td>
<tr>
</table>
<input type="hidden" name="counter" value="<%=count%>">
<input type="hidden" name="counter2" value="<%=count2%>">
</form>
</body>
</html>

我一直在尝试javascript validation.actually我一直在通过bean类从数据库中获取值,并且我已经用html表格式中的四个选项显示了问题。当我从四个选项中选择正确的选项时,我的count2变量[count2变量必须在获得正确响应时增加其值]不会递增.can有人告诉我我的 CLICKFORM 函数有什么问题当它从选项

获得正确的响应时,它不会递增count2值

1 个答案:

答案 0 :(得分:0)

首先,您需要了解服务器端和客户端之间的区别。你在里面的所有代码&lt; %%&gt;无论如何它总是被执行,它是服务器端代码。 count2的值将始终是第二个if条件中的任何值,JavasScript在这里没有说。这就是为什么你永远不会得到理想的价值。

其次,你不能在客户端使用服务器端代码,因为JavaScript不存在count2并且始终为null