简体   繁体   English

在Ajax调用期间更新jQuery Item

[英]Updating jQuery Item during Ajax call

I am making an ajax call to send out a couple of emails when the user clicks a button. 当用户单击按钮时,我正在进行ajax调用以发送几封电子邮件。 I am trying to update a "Please wait..." div before and after the call with the status, as well as report any errors. 我正在尝试使用状态更新呼叫前后的“请稍候...” div,并报告任何错误。 The problem... is that the div doesn't actually update until the ajax call is complete. 问题是...直到ajax调用完成,div才真正更新。

If I comment out the ajax call the status update works fine, but fails if the ajax call takes place. 如果我注释掉ajax调用,则状态更新工作正常,但是如果进行了ajax调用,则失败。 My Windows programming experience tells me the div needs to be refreshed, but I'm not sure how this is done, or if this is the right method. 我在Windows上的编程经验告诉我div需要刷新,但是我不确定如何完成此操作或该方法是否正确。

For example: 例如:

$("#msgEmail").show();
$("#msgEmail").html("Preparing to send");
$.ajax({ blah blah blah...

Any pointers in the right direction will be much appreciated! 任何朝着正确方向的指针将不胜感激!

On nnnnnn's suggestion I started testing on other browsers. 在nnnnnn的建议下,我开始在其他浏览器上进行测试。 The problem occurs on Chrome and Safari, but works as expected on Firefox and SeaMonkey. 该问题在Chrome和Safari上出现,但在Firefox和SeaMonkey上可以正常使用。 It sure looks like he's right about this. 看来他对此是正确的。 Now I just need to figure out to implement setTimeout() in this scenario. 现在,我只需要弄清楚在这种情况下实现setTimeout()的方法。

Update: Code: .click(function() { $('#myTable :checkbox:checked').each(function() { 更新:代码:.click(function(){$('#myTable:checkbox:checked')。each(function(){

            sId = $(this).attr('cid');
            sName = $(this).attr('cname');

            ret = true;
            $("#msgImage").slideDown();

            sUpdate = 'Sending alerts to class: '+ sName;
            $("#msgEmail").slideDown();
            $("#msgEmail").html(sUpdate);

            sSubject = "Notificatiom";
            sMessage = $('#message').val();
            sData= "cid="+sId+'&sname='+sName+'&subject='+sSubject+'&message='+encodeURIComponent(sMessage);
            $.ajax({
              url: 'dostuff.php',
              data: sData,
              dataType: 'text',
              type: 'POST',
              async: false,
                success: function (msg) 
                {
                  if(msg >= '1')    
                  { 
                    ret = true;
                  }
                }
            });

        if(ret)
            $("#msgEmail").html('Finished sending alerts');


        $("#msgImage").slideUp();
        ret = false;
        return false;
    }) 

Place your ajax call in the callback for 'slideDown'. 将您的ajax调用放在“ slideDown”的回调中。 Set the message before calling 'slideDown'. 在调用“ slideDown”之前设置消息。 This will ensure your message is updated before the ajax call is sent.' 这样可以确保在发送ajax调用之前更新您的消息。”

sUpdate = 'Sending alerts to class: ' + sName;

$("#msgEmail").html(sUpdate);
$("#msgEmail").slideDown(function() {
    sSubject = "Notificatiom";
    sMessage = $('#message').val();
    sData = "cid=" + sId + '&sname=' + sName + '&subject=' + sSubject + '&message=' + encodeURIComponent(sMessage);
    $.ajax({
        url: 'dostuff.php',
        data: sData,
        dataType: 'text',
        type: 'POST',
        async: false,
        success: function(msg) {
            if (msg >= '1') {
                ret = true;
            }
        }
    });

});

I ll get you an on hw this stuff is going to be 我会给你一个关于这个东西的信息

$(document).ready(function(){
   $.ajax({
    url :'YOur url',
    .....
   .....

    ajaxSend :function(){
    $('#yourdiv').html('please wait...');
   }
   success:function(msg){
      if (msg >= '1') {
       $('#yourdiv').html('validated');
      }

   }
});

}):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM