【问题标题】:ajaxStart and ajaxStop not workingajaxStart 和 ajaxStop 不起作用
【发布时间】:2014-11-16 08:52:48
【问题描述】:

一开始,我在我的页面中尝试了ajaxStartajaxStop,它们都不起作用,代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script language="javascript" src="jquery-1.11.1.js"></script>
</head>
<body>
<form name="form" method="post" action="">
    <input id = "name" type="text" name = "name" value="">
    <div id = "check"></div>
</form>

<script language="javascript">
       $("#name").blur(function() {
           $("#name").ajaxStart(function() {
               $("#check").html("loading...");
               alert("loading...");
           });
           $("#name").ajaxStop(function() {
               $("#check").html("OK...");
               alert("OK...");
           });
           $.ajaxSetup({
               url : "check.php",
               type : "POST",
               success : function(data) {
                   $("#check").html(data);
               },
           });
           $.ajax({
               url : "checkname.php",
               type : "POST",
               data : {name : $("#name").val()},
               datatype : "text",
               beforeSend : function() {
                   console.log("beforeSend");
               },
               success : function(data) {
                   $("#check").html(data);
                   console.log("success");
               },
               error : function() {
                   $("#check").html("error");
                   console.log("error");
               },
               complete : function() {
                   console.log("complete");
               },
           });
       });
</script>
</body>
</html>

然后,我找到了一个答案stackoverflow.com/questions/4034252/jquery-ajaxstart-not-working 告诉我改用document,当我使用以下代码时,它可以工作

$(document).ajaxStart(function() {
    $("#check").html("loading...");
    alert("loading...");   
});

另外,我尝试了另一种方法,使用 jQuery 1.7.2,它适用于 $("#name").ajaxStart()

我的问题是:

1、为什么在 jQuery 1.11.1 中 $("#name").ajaxStart() 不起作用,与 1.7.2 相比有什么变化?

2、有没有一些网站可以告诉我jQuery各个版本的区别,详细点会更好。

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: javascript jquery ajax


    【解决方案1】:

    1) 如 jQuery 官方文档中的 here 所述, 从 jQuery 1.8 开始,.ajaxStart() 方法只能附加到文档。

    2) 对于您的第二次查询,jQuery 官方文档应该足够了 对于不推荐使用的东西,请转到here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多