我正在使用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>发布于 2014-01-09 06:11:32
是的,在此情况下:
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"));
}干杯
发布于 2014-01-09 06:12:54
你当然可以。根据你的需要定位div。尝试添加jquery ()方法以进行定位。例:
$(“#div_1”).css(“填充-顶部”,“20 top”);
有关详细信息,请参阅:http://api.jquery.com/css/
发布于 2014-01-09 06:24:35
你也可以这样做:
在PHP上:
if(){
$resp['div1']= "<div id='alert_one'>Hi there</div>";
$resp['div2']= "<div id='alert_two'>Something</div>";
}
echo json_encode($resp);jQuery:
success: function(response)
{
data = jQuery.parseJSON(response);
$("#div_1").fadeIn('slow').html(data.div1);
$("#div_2").fadeIn('slow').html(data.div2);
}https://stackoverflow.com/questions/21012741
复制相似问题