【发布时间】:2014-03-02 16:25:03
【问题描述】:
这可能吗?
一旦我的 jQuery.post 成功而不是只有一个成功回调,我就有两个。
例如
一旦我的表单成功发布了数据,一个名为“#msg”的空 div 将被赋予样式和内容并且一个名为“color-block”的空 div 被赋予样式。
到目前为止的代码
$('#form1').submit(function(e)
{
e.preventDefault();
$.ajax({
type: 'POST',
url: 'indextest1.php',
data: $("#form1").serialize(),
success: function(response) {
$('#msg').html("<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax.....</div>");
}
});
});
任何帮助或指针将不胜感激。
我尝试过但没有奏效的方法!
添加另一个回调
$('#form1').submit(function(e)
{
e.preventDefault();
$.ajax({
type: 'POST',
url: 'indextest1.php',
data: $("#form1").serialize(),
success: function(response) {
$('#msg').html("<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax</div>");
$("#colour-block").html("<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>bla bla</div>");
}
});
});
使用 Promise 接口
$('#form1').submit(function(e)
{
e.preventDefault();
var ajax = $.ajax({
type : 'POST',
url : 'indextest1.php',
data : $("#form1").serialize()
}).done(function(response) {
$("#msg").html('<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax while we go to work on your behalf, we\'ll keep you updated with information on our results and if you have any questions then we welcome your calls or emails on 078675675446 or isaaclayne@southwestcarfinder.co.uk</div>');
$("#colour-block").html('<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax while we go to work on your behalf, we\'ll keep you updated with information on our results and if you have any questions then we welcome your calls or emails on 078675675446 or isaaclayne@southwestcarfinder.co.uk</div>');
});
});
});
【问题讨论】:
-
你不能在同一个函数中更新你的第二个 div 吗?
-
我不知道,可能。我该怎么办?像这样? $('#msg' && '#color-block').html 我最近才开始使用/学习 JavaScript,所以不太确定。我查看了 api 文档,并没有提到类似的内容。
-
哦,我要添加一个新行吗? $('#colour-block').html............
-
@DanCundy 是的,看我的回答。
标签: javascript jquery html css