gpt4 book ai didi

javascript - .ajaxComplete 不工作

转载 作者:行者123 更新时间:2023-11-29 16:58:55 24 4
gpt4 key购买 nike

您好,我目前在触发 .ajaxComplete 函数时遇到问题,它应该可以在演示站点上运行。我从这里引用这个函数 http://www.bitrepository.com/a-simple-ajax-username-availability-checker.html

这是我的代码:

    <SCRIPT type="text/javascript">
<!--
/*
Credits: Bit Repository
Source: http://www.bitrepository.com/web-programming/ajax/username-checker.html
*/

pic1 = new Image(16, 16);
pic1.src = "loader.gif";

$(document).ready(function(){

$("#userID").change(function() {

var usr = $("#userID").val();

if(usr.length >= 4)
{
$("#status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');

$.ajax({
type: "POST",
url: "check.php",
data: "userID="+ usr,
success: function(msg){
$("#status").ajaxComplete(function(event, request, settings){

if(msg == "OK")
{

$("#userID").removeClass('object_error'); // if necessary
$("#userID").addClass("object_ok");
$(this).html('&nbsp;<img src="tick.gif" align="absmiddle">');
}
else
{
$("#userID").removeClass('object_ok'); // if necessary
$("#userID").addClass("object_error");
$(this).html(msg);
}

});

}

});

}
else
{
$("#status").html('<font color="red">The username should have at least <strong>4</strong> characters.</font>');
$("#userID").removeClass('object_ok'); // if necessary
$("#userID").addClass("object_error");
}

});

});

//-->
</SCRIPT>

我曾尝试提醒 msg ,check.php 能够返回 msg 值,但它停在 .ajaxComplete() 那里它不会触发 .ajaxComplete() 之后的函数。请指导我完成这个。谢谢

最佳答案

来自document

As of jQuery 1.8, the .ajaxComplete() method should only be attached to document.

所以

$(document).ajaxComplete(function(event, request, settings){
});

但是在你的代码中,不需要使用ajaxComplete,成功回调只会在ajax调用结束时被调用,所以你可以直接在其中检查msg的值。

        $.ajax({
type: "POST",
url: "check.php",
data: "userID=" + usr,
success: function (msg) {
if (msg == "OK") {
$("#userID").removeClass('object_error'); // if necessary
$("#userID").addClass("object_ok");
$(this).html('&nbsp;<img src="tick.gif" align="absmiddle">');
} else {
$("#userID").removeClass('object_ok'); // if necessary
$("#userID").addClass("object_error");
$(this).html(msg);
}
}
});

关于javascript - .ajaxComplete 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29890877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com