2014-09-01 3 views
0

Это моя страница JSP:предупреждение показывает пустое значение в javascript?

<!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;

Edit ---------------

Это мой сервлет:

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); 
     } 
} 

}

+0

предупреждает ли «если» или «еще»? –

+0

@johnSmith да он возвращает, если или нет – AK2

+0

@ Andy нет, это не дубликат. Я получаю ответ от сервлета как найденный или не найден. – AK2

ответ

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"); 
       } 
      }); 
} 
+0

Извините, но не работает – AK2

+0

Вы видели предупреждение? 'alert (" if ")' или 'alert (" else ")' – Hamix

+0

Да, я вижу 'alert (" if ") или alert (" else ")' – AK2

Смежные вопросы