2015-04-22 45 views
0

當點擊某個圖像時,我想使用jQuery將一些內容添加到其他頁面。我有一個名爲product.php的通用頁面,在此頁面上添加的內容取決於在主頁(home.php)上點擊的圖像。在product.php我想添加一個div的內容,其編號爲#product如何在點擊鏈接時將內容添加到頁面,然後使用jQuery重定向到該頁面?

但是當圖片被點擊時,它會重定向到product.php的未修改頁面。在想要的內容被加載後有沒有辦法重定向到product.php頁面?

$('.product img').click(function() { 
    // it gives the right index   
    var index = $(".product img").index(this); 
    // it works well 
    var myProduct = 'snippet-product'+ (index + 1)+'.php'; 
    $("#product").load(myProduct); 
    // it redirects to the 'product.php' without the new content 
    window.location.href = 'product.php'; 
    return false; 
}); 
+1

你可以附加一些參數的URL。類似於window.location.href ='product.php'+'?param' – lharby

+0

是否要將內容加載到#product div並導航到另一頁? – lharby

+0

是的我想將內容加載到product.php上特定的#product div中,然後我想將其重定向到product.php頁面並能夠看到加載的內容 –

回答

0

我不知道如果我明白你的問題,但你需要像一個位置重定向:window.location.href = 'product.php?product=' + index;爲@lharby說,然後改變你的PHP代碼捕捉$_REQUEST["product"]並作出switchif到根據$_REQUEST["product"]值顯示內容。


你 「最初的問題」 是:

  • 第1步:加載內容到頁面。
  • 第2步。前往目的地product.php。

如果您重新加載頁面(或更改位置),則使用jQuery加載的所有內容都會消失。你懂我的意思嗎?

我真的不知道你想要什麼。

您是否只想更改URL但不重新加載頁面?請多說明一下你想要做什麼。

對不起,我的英語...

+0

我的主頁上有一張圖像列表。當我點擊其中任何一個時,我需要重定向到product.php。問題是,product.php只是一個框架頁面,並基於點擊圖像的索引,我想加載一個特定的內容在這個頁面上。 –

0

您可以將具體的產品信息發送給「product.php頁面的URL作爲查詢字符串:

window.location.href = 'product.php?index=' + index; // <-something like this 

然後在你的PHP使用$_GET['index']退回產品的指標:

if (isset($_GET['index'])) 
    $productIndex = $_GET['index']; 

然後使用$productIndex在PHP腳本查找併產生相應的內容顯示。

相關問題