1. 程式人生 > >【java】ajax判斷文字框是否為空

【java】ajax判斷文字框是否為空

方法返回的是1或者0,ajax裡面不能返回

在form中先寫驗證方法

<form action="<%=request.getContextPath() %>/Student/addStudent" method="post" onSubmit="return beforeSubmit(this);">

ajax

function beforeSubmit(form){
    if(form.studentname.value==''){
        alert("學生姓名不能為空!");
        form.studentname.focus();
        return false;
    }
    if(form.studentnumber.value==''){
        alert("學生學號不能為空!");
        form.studentnumber.focus();
        return false;
    }
    if(form.studentcommunication.value==''){
        alert("學生聯絡方式不能為空!");
        form.studentcommunication.focus();
        return false;
     }
var index;
$.ajax({
type:"post",
async:false,
url:'${path}/Student/isExistNum?studentnum='+form.studentnumber.value,
cache:false,
success:function(data){
index=data;
}
}); 
  if(index==1){
  alert("學號不能重複!");
  return false;
  }else{
  return true; 
  }
}