我正在使用 jquery 发送表单,我想在不同的地方显示来自 php 页面的 html 响应。我的意思是,我正在呼应不同的 div,并且我想在一个位置显示一个 div,在另一个位置显示另一个 div。
html
<div id="error"></div>
js
success: function(response)
{
$("#error").fadeIn('slow').html(response);//this displays both divs on same place
}
php
if(){
echo "<div id='alert_one'>Hi there</div>";
echo "<div id='alert_two'>Something</div>";
}
我可以做类似的事情吗
success: function(response)
{
$("#div_1").fadeIn('slow').html('div_id_from_php');
$("#div_2").fadeIn('slow').html('div_id_from_php');
}
<div id="div_1"></div>
<div id="div_2"></div>
最佳答案
是的,这样:
success: function(response)
{
var $divs = $("<div>" + response + "</div>");
$("#div_1").fadeIn('slow').html($divs.find("#alert_one").html());
$("#div_2").fadeIn('slow').html($divs.find("#alert_two").html());
}
或者像这样(可能更干净):
success: function(response)
{
var $divs = $("<div>" + response + "</div>");
$("#div_1").fadeIn('slow').empty().append($divs.find("#alert_one"));
$("#div_2").fadeIn('slow').empty().append($divs.find("#alert_two"));
}
干杯
关于javascript - 使用jquery在不同位置显示来自php的不同div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21012741/