【问题标题】:Ajax load "Comments" onclickAjax 加载“评论”onclick
【发布时间】:2015-05-19 22:06:38
【问题描述】:

我需要一些帮助,我有一个数据库:“ID | PLZ | Country | Author | Comment”

使用 Jquery,我设法显示/隐藏“评论”字段。

现在我尝试用 ajax onclick 加载评论,所以如果你点击“显示 cmets”按钮,ajax 应该从特定 id 加载评论。

我知道我还需要更改 jquery 代码以从正确的 id 获得正确的评论,但我不知道该怎么做。

这是我的代码:

查询:

$(document).ready(function(){
        $(".productDescription").hide();
        $(".show_hide").show();
        $(".hide_show").hide();

    $('.show_hide').click(function(){
    $(this).parent().find('.productDescription').slideToggle();
         $(this).parent().find(".show_hide").hide();
        $(this).parent().find(".hide_show").show();
    });

 $('.hide_show').click(function(){
    $(this).parent().find('.productDescription').slideToggle();
         $(this).parent().find(".show_hide").show();
        $(this).parent().find(".hide_show").hide();

    });

});

</script>

PHP:

<?php 
$abfrage = "SELECT * FROM ufo";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis))
    {

echo '
<div class="product clearfix">
    <h4>ID:'.$row->id.'|PLZ:'.$row->PLZ.'|Country:'.$row->Country.'</h4>
    <a href="#" class="show_hide">Show Comments</a>
    <a href="#" class="hide_show">Hide Comments</a>
    <div class="productDescription">
       <p>Comment:'.$row->Comment.'</p>
    </div>
</div>
<br>';

    }

?> 

如果有人可以帮助我,我将不胜感激。

久违了

【问题讨论】:

    标签: jquery ajax


    【解决方案1】:

    我会给你一个简单的例子。您没有提供视图,所以我只是给事物起了个名字。

    $(".someButton").click(function(){
        $.get("/urlToMethod?id=" + idAssociatedWithButton,function(result){
            //do stuff with result
        })
    })
    

    基本上当你的按钮被点击时,它会触发里面的代码。在单击内部,我有一个 get 调用,它将调用指定 URL 上的某些 Web 方法,该 URL 接受一个 ID。所以一个 url 可以是 /home/getComment?id=24。这意味着它调用了一个名为 getComment 的方法并传入了一个名为 id 的变量,其值为 24。这样您就可以从数据库中获取您的评论数据。 get 调用函数内的结果变量是从 get 方法返回的值。

    这里是有关使用 Jquery http://api.jquery.com/jquery.ajax/ 进行 ajax 调用的更多信息。 有很多方法可以做到这一点,这只是开始的一种方法。

    【讨论】:

    • 嘿,谢谢你的回复,我现在需要调用一个 .php 文件来获取 cmets 吗?现场演示:twitch-smileys.com/ufo
    • 是的,基本上你通过 url 调用一个 php 方法,然后在那个 php 中调用数据库。我对 php 不熟悉,因此无法帮助您了解如何构建它,但我确信网络上有很多示例。
    猜你喜欢
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多