2015-01-09 50 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放在一起。 –