【问题标题】:help with jquery ajax success event帮助 jquery ajax 成功事件
【发布时间】:2011-05-18 14:42:55
【问题描述】:

我的更新按钮和 jquery ajax 有问题。现在,当我单击更新按钮时,它会将所有更新的数据保存到数据库中。我的目标是如果更新成功,我想向上滑动一条消息。我正在查看 ajax 帖子并使用成功事件似乎可以工作,但我不知道如何合并它。我该怎么做?会是这样吗?

     $(document).ready(function(){
        $('#divSuccess').hide();

        $('#btnUpdate').click( function() {
        alert('button click');
            $.ajax({ 
                  url: "test.aspx", 
                  context: document.body, 
                  success: function(){ 
                    $('#divSuccess').show("slide", { direction: "down" }, 3000);
                    $('#divSuccess').hide("slide", { direction: "down"}, 5000);
                  }
                }); 
        });
    });

【问题讨论】:

  • 为什么这么复杂?对于您的第一次尝试,您应该使用一个简单的警报框。如果可行,请创建一个默认可见隐藏的简单 div。在成功事件中,您打开此对象(使用 .show())

标签: javascript jquery


【解决方案1】:

查看question 以获取有关如何处理成功事件的示例。希望这会有所帮助!

【讨论】:

    【解决方案2】:
    $("#targetDiv").load("page.php",$("#form").serializeArray(),function (response) 
                {
                  if (response == '0' && response != '')
                   alert('Request not sent to server !\n');
                  else if(response == '-1')
                   alert('Please write some more !\n');
                  else
                  {
                    alert("success! ");
                  }
                }
             );
    

    我已经回显 0 和 -1 表示失败,其他表示成功

    【讨论】:

    • 错字 $("#form) => $("#form") ;)
    【解决方案3】:

    在jquery的post函数中,可以执行一些回调函数。

        function (data, textStatus) {
          // data could be xmlDoc, jsonObj, html, text, etc...
          this; // the options for this ajax request
          // textStatus can be one of:
          //   "timeout"
          //   "error"
          //   "notmodified"
          //   "success"
          //   "parsererror" 
          // NOTE: Apparently, only "success" is returned when you make
          // an Ajax call in this way. Other errors silently fail.
          // See above note about using $.ajax.
        }
    

    http://docs.jquery.com/Post

    【讨论】:

      【解决方案4】:

      至少使用 jQuery 1.5,您已经获得了延迟对象和 AJAX 事件的新语法(包括 success)。

      var $ajaxcall = $.ajax({
          url : 'myurl.svc/somemethod',
          data : '{ somedata : "sometext" }'
      });
      
      $ajaxcall.success(function() {
          // do something on successful AJAX completion
      });
      

      当然,您也可以将其链接起来,并按照$.ajax().success() 之类的方式调用。

      如果您有兴趣阅读更多内容,只需 wrote a blog post on it myself

      【讨论】:

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