jQuery $ .ajax成功仅运行一次 [英] jQuery $.ajax success runs for one time only

查看:105
本文介绍了jQuery $ .ajax成功仅运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施单颗星评分(即赞"按钮).

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

我想更改(切换)星图.它似乎唯一的问题是,在使用$ .ajax时,在成功:" 部分上,将应用 src 属性(或其他任何内容,例如.css) 一次(第一次)!实际上,客户端必须刷新页面才能查看最新的星形图像/状态(从数据库加载).

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).

这是代码:

<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>


php文件回显一个文件名,该文件名应替换为src属性(例如star-on.png或star-off.png)

所以我认为问题是:为什么成功:功能"仅触发一次?

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

推荐答案

我终于意识到问题出在哪里.这是由于服务器端文件(php)所致,因为它总是评估从客户端文件给出的静态数据.我需要做的就是刷新我的php文件中的切换触发器.

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.

这篇关于jQuery $ .ajax成功仅运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆