function Checkform () {var pass;
var msg;
var url= "${ctx}/crm/contact.action?method:checkform";
var Qq=document.getelementbyid ("Contact_qq"). Value;
var Mobile=document.getelementbyid ("Contact_mobile"). Value;
var Email=document.getelementbyid ("Contact_email"). Value;
$.ajax ({url:url, async:false, type: "Post", DataType: "JSON",
Data: {qq:qq, Mobile:mobile, Email:email
}, Success:function (data) {pass=data.pass;
msg=data.msg; if (pass) {alert ("Save succeeded.
");
}else{alert (msg);
}
}
});
if (pass) return true;
else return false; } <button type= "Submit" class= "btn btn-sm btn-primary savabtn" Name= "metHod:save "onclick=" return Checkform () "> Save </button>
The
Important step is async:false, because Ajax is asynchronous by default, that is, the default is Async:true. When set to False, the pass value returned to the foreground in Ajax can be used in the function Checkform.
in action,
public void Checkform () {string mobile= (string) getparameter ("mobile", String.class);
String email= (String) getparameter ("email", string.class);
String qq= (String) getparameter ("QQ", String.class);
Boolean pass=true;
List mobilelist = Contactmanager.find ("From Contact where mobile=?", mobile);
List emaillist = Contactmanager.find ("From Contact where email=?", email);
List qqlist = Contactmanager.find ("From Contact where qq=?", QQ); Mobile is not empty and size is not 0, that is, duplicate if ((! Mobile ==null | |
Mobile.equals (""))) && mobilelist.size ()!=0) {pass=false;
msg+= "mobile phone Number:" +mobile+ "repeat"; } if ((!) ( Email ==null | |
Email.equals (""))) && emaillist.size ()!=0) {pass=false;
msg+= "Email:" +email+ "repeat"; } if ((!) ( QQ ==null | |
Qq.equals (""))) && qqlist.size ()!=0) {pass=false;
msg+= "QQ number:" +qq+ "repeat"; } jsonobject resultobject = new Jsonobject ();
Resultobject.put ("Pass", pass); if (! ( msg ==null | |
Msg.equals (""))) {Resultobject.put ("msg", Msg.substring (4));
}else{resultobject.put ("msg", MSG);
} System.out.println ("==========");
SYSTEM.OUT.PRINTLN ("pass" +pass);
System.out.println (Resultobject.tostring ());
Responsewrite (Resultobject.tostring ()); }