大佬教程收集整理的这篇文章主要介绍了jQuery get函数返回true / false,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
$(document).ready(function(){ //global vars var name = $("#username"); var email = $("#email"); function usernameExists() { $.get("register.PHP",{ check: 1,username: name.val(),email: email.val() },function(m) { if(m==1) { return false; } else { return true; } }); } });
Firebug在调用此函数时显示正确的响应,但它不返回任何内容……(此$.get(…)函数已在函数usernameExists()之外进行了测试,但没有返回,并且它完美地工作).
有什么问题以及如何解决?
$(document).ready(function(){ //global vars var form = $("#register"); var name = $("#username"); var email = $("#email"); $.get("register.PHP",// Have this callBACk take care of the rest of the submit() function(m) { if(m==1) { form.submit(function(){ return false; }); } else { form.submit(function(){ if(validatename() & validateEmail() & validatePass1() & validatePass2()) return true else return false; }); } } ); function validatename(){ // some check here } // and other functions });
即使它确实尝试从$.get()返回响应,它也不会起作用,因为调用是异步的,所以当收到响应时,任何使用返回值的代码都可能已经执行.
function usernameExists() { $.get("register.PHP",function(m) { someOtherFunction(m==1); }); } function someOtherFunction(parameter) { // The parameter will be true or false // depending on the value of m==1 }
根据您的评论更新.
可能更好的只是将$.get()带入submit(),但保持原始想法,这就是它的外观.
form.submit(function(){ // After usernameExists() is called,we need to hand off // the rest of the execution to that function since // this one will be done execuTing before the get() // response is received usernameExists(); return false; }); function usernameExists() { $.get("register.PHP",// Have this callBACk take care of the rest of the submit() function(m) { if(m==1) { // do something if true } else { // do something if false } } ); }
解释同步与异步javascript的乐趣
Javascript代码通常是同步执行的.这只意味着它一次执行一行,或者一行必须在下一行可以触发之前完成执行.
var greeTing = "hi there"; // set the greeTing variable alert( greeTing ); // the alert won't fire,// until the prevIoUs line finished successfully
这使事情变得非常好和可预测.但该规则有一些例外.一个值得注意的例外是AJAX调用.
你的$.get()是一个AJAX调用的例子. AJAX中的“A”代表异步,这意味着它不会阻止下一行代码执行.
分支是当你执行一个$.get()需要花费(例如)1秒来完成时,$.get()之后的任何代码都会在$.get()收到它之后很久完成.响应.
以前的问候语为例,但这次使用的是AJAX.
var greeTing; // will hold the response from our AJAX call $.get('some/path/to/data.PHP',function( m ) { greeTing = m; // populate the greeTing variable with the data returned } ); alert( greeTing ); // Will alert "undefined" instead of the data that was returned // because the $.get() code above is asynchronous,which allows // the code below it (the alert in this casE) to conTinue // execuTing.
正如您所看到的,警报(问候语)将在收到$.get()响应之前很久就已执行,因为$.get()是异步的,并且在等待其数据时不会暂停执行链.
要解决此问题,您可以将alert()放在$.get()的回调中,以便在收到响应之前它不会运行.
var greeTing; // will hold the response from our AJAX call $.get('some/path/to/data.PHP',function( m ) { greeTing = m; // populate the greeTing variable with the data returned alert( greeTing ); // Now the alert will give the expected result // because it is in the callBACk. } );
结果是,在您的代码中,一旦您调用$.get(),任何依赖于收到的响应的剩余代码都应该在回调内部进行.
将代码放在回调之外的唯一方法是将它放在自己的函数中,该函数从回调内部调用(就像我用我的原始答案做的那样).
代码运行方式的基本布局:
请记住,您不一定需要usernameExists()的单独函数.您可以将所有代码放在submit()中
form.submit(function() { // check to make sure input is valid **before** you send the AJAX if(validatename() & validateEmail() & validatePass1() & validatePass2()) { usernameExists(); // If valid,conTinue with the usernameExists() } return false; // We return false whether or not the content was valid,// in order to prevent the form from submitTing prematurely }); function usernameExists() { $.get("register.PHP",// Have this callBACk take care of the rest of the submit() function(m) { // If "m" is less than one,there were no exisTing users // so we can go ahead and post the data to the server if( parseInt(m) < 1 ) { // Here,you would need to manually do a post to // submit the data to the server $.post(url,data,callBACk,datatype ); } } ); }
以上是大佬教程为你收集整理的jQuery get函数返回true / false全部内容,希望文章能够帮你解决jQuery get函数返回true / false所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。