[英]How to add content to a page when clicking on a link and then redirect to that page using jQuery?
单击某个图像时,我想使用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;
});
我不知道我是否理解您的问题,但是您需要使用以下位置进行重定向: window.location.href = 'product.php?product=' + index;
如@lharby所说,然后更改您的PHP代码以捕获$_REQUEST["product"]
并进行switch
或者if
基于$_REQUEST["product"]
值显示内容。
您的“初始问题”是:
如果您重新加载页面(或更改位置),则jQuery加载的所有内容都会消失。 你懂我的意思吗?
我真的不知道你想要什么
您是否只想更改URL但不重新加载页面? 请多解释一些您想要完成的事情。
对不起我的英语不好...
您可以将特定的产品信息作为查询字符串发送到url中的“ product.php”页面:
window.location.href = 'product.php?index=' + index; // <-something like this
然后在您的PHP上,使用$_GET['index']
返回产品的索引:
if (isset($_GET['index']))
$productIndex = $_GET['index'];
然后在PHP脚本中使用$productIndex
查找并生成要显示的适当内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.