2015-01-09 40 views
0

我目前有一个div,点击后将数据发布到另一个页面并重新加载div。我想添加一个返回按钮(#backref)到新的div让我回到以前的股利。我发现了几个帖子,因此你添加display:none;到你想要浏览的div,但显然这是我的初始div,所以我不能使用该CSS。任何有关如何避免这个问题的建议将不胜感激!使用jQuery在div之间导航

$(document).ready(function() { 
$('.clickthrough2').click(function() { 
    // get car id 
    carId = $(this).attr('id'); // get the car id to access each class element under the car div container 

    $.post('referrer-ajax2.php', { 
     clickthrough: $('#car-'+carId+' .clickthrough').val(), 
     ref_date_from2: $('#car-'+carId+' .ref_date_from2').val(), 
     ref_date_to2: $('#car-'+carId+' .ref_date_to2').val() 
    }, 
    function (data) { 
     $('#car1').html(data); 
    }); 
});  
}); 

的index.php:我使用来实现这一

代码

<div id="car1" class="declined3 statdivhalf2"> 
<h4>Select Car</h4> 

<div class="statgrid"> 
    <?php 
    $result=$ mysqli->query($sql); 
    if($result->num_rows === 0) { echo 'No Cars in selected time period.'; } 
    else { while ($row = $result->fetch_array()) { 
    ?> 
<div id="car-<?php echo $row['car_id'];?>"> 
<input type="hidden" class="ref_date_from2" value="<?php echo $date_from; ?>" /> 
<input type="hidden" class="ref_date_to2" value="<?php echo $date_to; ?>" /> 
<input type="hidden" class="clickthrough" value="<?php echo $row['car_name'] ?>" /> 
<a><div id="<?php echo $row['car_id'];?>" class="clickthrough2 col-5-6"><?php echo $row['car_name'] ?></div></a> 
</div> 

    <div class="col-1-6"> 
     <?php echo $row[ 'quantity']; ?> 
    </div> 
    <?php } } ?> 
</div> 
</div> 

引荐-ajax2.php:

<div id="car1" class="declined4 statdivhalf2"> 
    <h4>Car Details</h4> 

<div class="statgrid"> 
    <?php 
    $result=$ mysqli->query($sql); 
    if($result->num_rows === 0) { echo 'No Cars in selected time period.'; } 
    else { while ($row = $result->fetch_array()) { 
    ?> 
    <div id="clickthrough2" class="col-5-6"><?php echo $row['car_details'] ?></div> 
    <div class="col-1-6"><?php echo $row[ 'quantity']; ?></div> 
    <?php } } ?> 
    <a><div id="backref">< Back</div></a> 
</div> 
</div> 

编辑:我曾尝试以下无法正常工作:

$('#backref').click(function() { 
    $('.declined4').hide(); 
    $('.declined3').show(); 
}); 

回答

1

而不是重新加载与新内容的div你可以创建一个新的div每次隐藏旧的。然后,您可以通过为每个div指定一个id或(可能更好)一个“data-”属性来遍历它们。

+0

您是否有示例?我以前没有听说过这种方法。 – n00bstacker

+0

你的意思是在这里用jQuery创建一个新的div:S。或者你的意思是更隐藏和取消隐藏 – n00bstacker

+0

在你的代码中,你正在做 函数(数据)$('#car1')。html(data); }); 更改该行以隐藏div,并将新的html与新的div放在一起。 –