如何在单击链接时向页面添加内容,然后使用jQuery重定向到该页面?

时间:2015-04-22 13:37:54

标签: javascript php jquery html

点击某个图片后,我想使用jQuery将一些内容添加到其他页面。我有一个名为product.php的通用页面,在此页面上,要添加的内容取决于在主页(home.php)上单击的图像。在product.php我尝试在ID为#product的div上添加内容。

但是当点击图片时,它会重定向到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;   
});

2 个答案:

答案 0 :(得分:0)

我不知道我是否理解你的问题,但你需要像@lharby所说的那样重定向:window.location.href = 'product.php?product=' + index;,然后改变你的PHP代码捕捉$_REQUEST["product"]并制作一个{ {1}}或switch根据if值显示内容。

您的“初步问题”是:

  • 第1步。将内容加载到页面。
  • 第2步。转到位置product.php。

如果您重新加载页面(或更改位置),则加载jQuery的所有内容都会消失。你知道我的意思吗?

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

您是否只想更改网址但不重新加载网页?请再解释一下你想要了解的内容。

抱歉我的英文......

答案 1 :(得分:0)

您可以将特定产品信息发送到产品.php' url中的页面作为查询字符串:

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

然后在PHP上使用$_GET['index']返回产品的索引:

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

然后在PHP脚本中使用$productIndex来查找并生成要显示的相应内容。