douxiajiao8445 2011-12-15 23:34
浏览 15
已采纳

jQuery - 如何使用不同的步骤进行响应?

I want to have an ajaxed form and when a user clicks submit it will submit the form values to a php file nothing fancy here and to that point anything is clear to me.

Now I want to have several responses from the server to shop in the response div.

For example the form is for writing a post and then my php file will post that post to 5 different forums and I want to show in the result exactly what it is doing like

Posting to forum 1... then clear that and show Posting to forum 2...

I want to show what the script is currently doing live in the jquery output div and my question is how to do that? Please tell me the code requirements in jquery as well as the ones in PHP.

Thanks in advance for your help

  • 写回答

1条回答 默认 最新

  • dongxuan2577 2011-12-16 00:01
    关注

    Here is a function that should accomplish this for you:

    //'step' indicates the current iteration of the posting loop, and 'limit' tells it after how many iterations to stop. If you want to post to 5 of 5 forums, set 'step' to 1 and 'limit' to 5.
    
    function submitSteps(step, limit)
    {
        if (step <= limit)
        {
        $.ajax({
            url: 'http://example.com',
            data: 'postTo='+step,
            beforeSend: function()
            {
                 if ($('#notice p:last').html().indexOf('problem') > -1)
                 {
                      $('#notice p:last').remove();
                 }
                 $('#notice').append('<p>Posting to forum '+step+'...</p>');   
            },
            success: function()
            {
                 $('#notice p:last').remove();
                 $('#notice').append('<p>Post to forum '+step+' complete!</p>');
                 window.setTimeout('submitSteps(step+1, limit)', 1000);   
            },
            error: function()
            {
                 $('#notice p:last').remove();
                 $('#notice').append('<p>There was a problem posting to forum '+step+'.</p>');
                 window.setTimeout('submitSteps(step, limit)', 1000);    
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮