如何让程序等待ajax结果继续处理?
我正在进行表单validation。 在表单检查function中,我调用了ajax函数来validation字段。 如果ajax的返回数据等于“No”,则该函数应该返回false。 但是,它永远不会等待ajax结果返回。 我该如何处理这个问题? 我的按钮代码:
jQuery代码:
function Checkform() { var result = true; $('input:password').each(function() { if ($(this).val() == "") { $(this).focus(); $(this).addClass('HighlightBorder'); alert("Please set password."); result = false; return false; } }); if (!result) return false; ...... $.post(rootPath + "/AjaxPages/SearchAddress.aspx", { "street": street, "city": city, "state": state, "zip": zip, "type": "DPV" }, function(data) { if (data == "No") { alert("Invalid company address, did you type in a wrong address?"); result=false; } }); return result; }
然后我修改了我的ajax代码
$.post(rootPath + "/AjaxPages/SearchAddress.aspx", { "street": street, "city": city, "state": state, "zip": zip, "type": "DPV" }, function(data) { if (data == "No") { alert("Invalid company address, did you type in a wrong address?"); return false; } else return true;
我想这次返回值是在数据出来后执行的,但是,它仍然不起作用。
ajax是异步的,因此您必须使用回调。
$.post(rootPath + "/AjaxPages/SearchAddress.aspx", { "street": street, "city": city, "state": state, "zip": zip, "type": "DPV" }, function(data) { if (data == "No") { alert("Invalid company address, did you type in a wrong address?"); some_callback_function();//callback } });