我正在尝试使用 AJAX 生成 PHP,以便根据单击的链接的 ID 在 div 内显示。 (我还没有处理过 HTML 格式,只是想先弄清楚这一点)
我的代码:
<script type="text/javascript">
jQuery(document).ready(function()
{
$(".myClass h3").click(function(){
var clickID = $(this).attr('id'); //For sake of argument lets say id = 1.
$.ajax({
url: 'landingPage.php',
data: {ProductIDNumber: clickID},
success: function(html) {
$('#container').append(html);
}
});
});
});
</script>
我已经处理过让 PHP 在 url 为 landPage.php?ProductIDNumber=1 时运行,并且一切正常,我只是不太明白如何返回生成的 HTML。
进行了编辑并发表了评论。
最佳答案
您需要包含success
,它在收到响应后运行,类似于;
<script type="text/javascript">
jQuery(document).ready(function()
{
$(".myClass h3").click(function(){
var clickID = $(this).attr('id'); //For sake of argument lets say id = 1.
$.ajax({
url: 'landingPage.php',
data: {
ProductIDNumber : clickID
},
success: function(html) {
// Do what you need to here with 'html'
$('#container').append(html);
}
});
});
</script>
正如阿尔瓦罗所说,数据
的结构也不正确
Here's a link到 jQuery 文档,您可以在其中找到有关您拥有的参数和选项的更多信息(例如处理失败等)
关于php - 我想使用 AJAX 根据单击的链接的 ID 动态生成布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23301396/