Javascript新手,如果这是一个愚蠢的问题,我们深表歉意!
我很难解释这一点,但我有一个简单的竖起大拇指系统,如下所示:
//thumbs up down reviews
$(function addThumbs() {
$(".vote-up").click(function() {
var review_id = $('#review_id').val();
var user_id = $('#user_id').val();
if (user_id == '') {
alert('no user id');
} else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
$.ajax({
type: "POST",
url: "ajax-thumbsup.php",
data: {
"user_id": user_id,
"review_id": review_id //we are passing the name value in URL
},
cache: false,
success: function(html) {
$('#vote-up').attr('src', 'img/upgreen.png');
}
});
}
return false;
});
});
$(function addThumbsDown() {
$(".vote-down").click(function() {
var review_id = $('#review_id').val();
var user_id = $('#user_id').val();
if (user_id == '') {
alert('nouserid');
} else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
$.ajax({
type: "POST",
url: "ajax-thumbsdown.php",
data: {
"user_id": user_id,
"review_id": review_id //we are passing the name value in URL
},
cache: false,
success: function(html) {
$('#vote-down').attr('src', 'img/downred.png');
}
});
}
return false;
});
});
我的 html 看起来像这样:
<div class="row">
<div class="thumbs">
<input type="hidden" id="review_id" value="11" />
<input type="hidden" id="user_id" value="5" />
<div id="pluses">0</div> <div id="progressdiv"></div>
<a><img id="vote-up" class="vote-up" src="img/up.png" alt="thumbs up" /> </a>
<a><img id="vote-down" class="vote-down" src="img/down.png" alt="thumbs down" /> </a>
<div id="minuses">0</div>
</div>
comments/likes
</div>
<div class="row">
<div class="thumbs">
<input type="hidden" id="review_id" value="12" />
<input type="hidden" id="user_id" value="5" />
<div id="pluses">0</div> <div id="progressdiv"></div>
<a><img id="vote-up" class="vote-up" src="img/up.png" alt="thumbs up" /> </a>
<a><img id="vote-down" class="vote-down" src="img/down.png" alt="thumbs down" /> </a>
<div id="minuses">0</div>
</div>
comments/likes
</div>
如果我单击第二个竖起大拇指,第一个变成绿色,我如何告诉 javascript 哪个 img#vote-down 需要更改?