php - 我想使用 AJAX 根据单击的链接的 ID 动态生成布局

标签 php jquery ajax

我正在尝试使用 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/

相关文章:

php - 在 Codeigniter 中填充表单数据和验证数据的更简单方法?

php - 如何检查 Mysql 行中的文本?

php - 通过计算 PHP : 20 values => X values 中的平均值来减小数值数组的大小

jquery - 使用 jQuery 从加载的内容中删除脚本标签

javascript - 类型错误 : Cannot read property 'value' of null

php - 如何阻止 Google adsense 链接到我服务器上的文件?

javascript - jQuery Scrollify - 第一部分忽略偏移量

javascript - 使 slider 动态

javascript - 对更新内容的 AJAX 请求进行限速以避免重复

javascript - Rails 远程链接更新记录,同样打开链接