警报在javascript中显示空白值?

时间:2014-09-01 11:07:56

标签: java javascript html jsp

这是我的jsp页面:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    <script src="newjavascript.js"></script>
  </head>
   <body>
 Name:         <input type="text" value="" id="t1"><br>
 Age:          <input type="text" value="" id="t2"><br>
 Rollno:       <input type="text" value="" id="t3"><br>
 Address:      <input type="text" value="" id="t4"><br>
<div id="er" style="color: red;"></div>
<input type="submit" value="Submit" onclick="validation()">
 </body>
</html>

这是我的javascript:

var st_roll_no="";

function validation()
{
if (document.getElementById("t1").value.replace(/^\s+|\s+$/g, "") === "")
{
    document.getElementById("er").innerHTML = "please select name";
}
else if (document.getElementById("t2").value.replace(/^\s+|\s+$/g, "") === "")
{
    document.getElementById("er").innerHTML = "please select age";
}
    else if (document.getElementById("t3").value.replace(/^\s+|\s+$/g, "") != "")
{
    fetch_rollno(document.getElementById("t3").value);
            alert("After fetching the result is "+st_roll_no);
}
      else if (st_roll_no.replace(/^\s+|\s+$/g, "") === "found")
{
    document.getElementById("er").innerHTML = "Please enter address";
}
   else if (document.getElementById("t4").value.replace(/^\s+|\s+$/g, "") === "")
{
    document.getElementById("er").innerHTML = "Please enter address";
}
    else
    {
        alert("success");
    }
}

function fetch_rollno(rollno)
 {
 $.ajax({
            type: "POST",
            url: 'validate_rollno',
            data: {rollno: rollno},
            success: function(result) 
            { 
                alert("result from database is "+result);             
            if (result == "found")
                {
                    alert("if");
                    st_roll_no="found";
                    alert(st_roll_no);

                }
                else
                {
                     alert("else");
                     st_roll_no="notfound";
                     alert(st_roll_no);

                }

            }
        });
    }

从数据库中获取rollno并设置全局变量st_roll_no之后。此行

 alert("After fetching the result is "+st_roll_no);

正在打印空白提醒。它应该打印found or notfound;

编辑---------------

这是我的servlet:

public class validate_rollno extends HttpServlet {


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
       PrintWriter out=response.getWriter();
       String Status="";
       String rollno=request.getParameter("rollno");
       classes.query q=new classes.query();
       java.util.List li=q.select("select name from student where rollno='"+rollno+"'");
       java.util.Iterator it=li.iterator();
       if(it.hasNext())
       {
           Object oo=it.next();
           Status="found";
           out.println(Status);
       }
       if(li.size()==0)
       {
            Status="not found";
            out.println(Status);
       }
}

}

1 个答案:

答案 0 :(得分:0)

试试这个

alert("After fetching the result is "+(st_roll_no? "found" : "notfound"));
  

UPDATE1:

function fetch_rollno(rollno)
{
     var st_roll_no="After fetching the result is ";    
     $.ajax({
                type: "POST",
                url: 'validate_rollno',
                data: {rollno: rollno},
                success: function(result) 
                {
                    st_roll_no+= (result == "found") ?"found" : "notfound";
                    alert("st_roll_no");
                }
            });
}