jQuery $.ajax success runs for one time only

落花浮王杯 提交于 2019-12-13 05:37:26

问题


I'm trying to implement a single-star rating (i.e. a like button).

I want to change (toggle) the star image. The only problem it seems to have is that while using $.ajax, on "success:" part, the src attr (or anything else really, like .css) applies for one (the first) time ONLY! In fact, the client has to refresh the page to see the latest star image/status (which loads from the db).


Here's the code:

<script language="javascript">
    // Ajax: Star
    $("#p<?php echo $pID;?>").find('.star').click(function (e) {
        e.preventDefault();

        $.ajax({
        type: "POST",
        url: "./ajax.php",
        data: "pID=<?php echo $pID;?>",
        cache: false,

        success: function(html)
        {
            $("#s<?php echo $pID;?>").attr("src",html);
        }
        });
    });
    // END OF: Ajax: Star
</script>

the php file echos back a filename which is meant to be replaced with the src attribute (e.g. star-on.png OR star-off.png)

So I think the question is: Why the "success: function" triggers only once?


回答1:


I finally realized what the problem is. It is due to the server side file (php) as it always evaluates the static data given from the client side file. All I need to do is refreshing the toggle's trigger on my php file.



来源:https://stackoverflow.com/questions/8865836/jquery-ajax-success-runs-for-one-time-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!