我有这个 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/