2012-04-16 82 views
0

大家好,我正在尝试将jQuery AJAX整合到我的多步骤表单中,以便它将某个div更新为process.php页面上的某个div,但它始终将结果加载到新一页。如何在不刷新页面的情况下更新div?多步骤表单上的jQuery ajax

这是我使用jQuery代码:

$.ajax({ 
    url: 'process.php', 
    data: $('form[name="booking"]').serialize(), 
    dataType: 'html', 
    type: 'POST', 
    success: function(data) { 

     // this is the bit that needs to update the div 

     $('#last-step').fadeOut(300, function() { $('#result').html(data).fadeIn(300); 
    }, 
    complete: function (data) { 
     // your code here 
    }, 
    error: function (url, XMLHttpRequest, textStatus, errorThrown) { 
     alert("Error: " + errorThrown); 
    } 
}); 

这是我的多步骤形式的代码:http://jsfiddle.net/xSkgH/47/

在提前感谢

回答

1

我没有看到一个div在标记称为result。所以可能你需要在你显示的最后一个div中显示你的结果。而且你也缺少})。下面的代码应该可以工作,

$.ajax({ 
    url: 'process.php', 
    data: $('form[name="booking"]').serialize(), 
    dataType: 'html', 
    type: 'POST', 
    success: function(data) { 

     // this is the bit that needs to update the div 

     $('#last-step').fadeOut(300, function() { 
       $('#last-step').html(data).fadeIn(300); 
     }); 
    }, 
    complete: function (data) { 
     // your code here 
    }, 
    error: function (url, XMLHttpRequest, textStatus, errorThrown) { 
     alert("Error: " + errorThrown); 
    } 
}); 
+0

结果div存储在process.php文件中。我测试了你的代码,它似乎返回相同的问题。结果出现在新页面中,并且div未被更新。不知道我做错了什么。 – CJS 2012-04-16 12:41:49

+0

process.php是处理您的ajax请求的服务器页面。所以你应该在你的客户端屏幕(你有表单的页面)将结果更新为div。确保你没有其他脚本错误。你是否在某处点击提交按钮? – Shyju 2012-04-16 12:51:04

+0

这是否意味着必须创建所有表单clientside?即使是divs? – Tom 2012-07-16 09:01:02