javascript - jQuery - 单击一个元素会触发另一个元素

标签 javascript jquery html image

我有这个 jQuery 文件,但是 vote_up 点击处理程序 一直与 vote_down 点击处理程序 冲突,当我点击 vote_down 元素时,它会更改 vote_up 元素:

jQuery 脚本:

$(document).ready(function () {
    $("a.vote_up").click(function () {
        //get the id
        var the_id = this.id.split('_').pop();
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_up&id=" + the_id,
            url: "ajax/votes.php",
            success: function (msg) {
                $("span.vote_count#" + the_id).html(msg).fadeIn();
                $("#" + the_id + " img").attr("src", "img/uparrowActive.png");
            }
        });
    });
});
$(document).ready(function () {
    // the vote down function
    $("a.vote_down").click(function () {
        //get the id
        var vote_id = this.id.split('_').pop();
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_down&id=" + vote_id,
            url: "ajax/votes.php",
            success: function (msg) {
                $("span.vote_count#" + vote_id).html(msg).fadeIn();
                $("#" + vote_id + " img").attr("src", "img/downarrowActive.png");
            }
        });
    });
});

html:

<a href='#' class='vote_up' id="id_23"><img src="img/uparrow.png" /></a>
<a href='#' class='vote_down' id="id_23"><img src="img/downarrow.png" /></a>

jQuery 和 ajax 请求工作正常,但 src 的更改是问题所在,因为当我单击“投票向下”时,它会更改 vote_up 图像!!

最佳答案

您不能对两个不同的元素使用相同的“id”值。

关于javascript - jQuery - 单击一个元素会触发另一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3868493/

相关文章:

html - 我还应该为 iframe 使用 seamless 属性吗?

HTML img src 返回 404 未找到错误,尽管一切正常

javascript - 在页面上第二次播放时 YouTube 视频出现在错误的位置

javascript - JQuery 函数返回错误数量的像素

javascript - 无法在 JSON 对象内动态添加值

javascript - 单击时从输入中删除字符串? jQuery

php - 根据选择器和声明(key => value)从数据库中显示 CSS

javascript - JS : displaying an infinitely long coordinating system (grid)

javascript - child 没有根据 Vue.js 中的 props 进行更新

javascript - 来自 AJAX 请求时,toggleClass 不起作用