2015-04-22 44 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脚本查找并产生相应的内容显示。

相关问题