![](/img/trans.png)
[英]How do I enable a Submit button and change it's value after getting a successful ajax response in Django?
[英]How to change html class after ajax successful response?
我想切换投票箭头类,就像stackoverflow的投票箭头一样。 对于已登录的用户,如果已经投票,则html为:
<a href="#vote" id="vote-up-%1$d" class="vote vote-up-on"></a>
<a href="#vote" id="vote-down-%1$d" class="vote vote-down-off"></a>
用户点击向下箭头后,如果响应成功,如何将“投票结束”更改为“投票结束”,并将“投票结束”更改为“投票结束”?
嗯,这与SA投票的工作方式相同 - 它可能更简洁但我真的需要重新开始工作;-)
<a href="#vote" id="vote-up-%1$d" class="vote vote-up">vote up</a>
<span class="votes">0</span>
<a href="#vote" id="vote-down-%1$d" class="vote vote-down">vote down</a>
和jquery:
$(document).on('click', '.vote-up', function () {
if(!$(this).hasClass('vote-locked')){
var votes = parseInt($(this).parent().find('.votes').text());
if(!$(this).hasClass('vote-off')){
$(this).addClass('vote-off');
$(this).nextAll('a').addClass('vote-locked');
votes++;
}
else{
$(this).removeClass('vote-off');
$(this).nextAll('a').removeClass('vote-locked');
votes--;
}
$(this).parent().find('.votes').text(votes);
}
});
$(document).on('click', '.vote-down', function () {
if(!$(this).hasClass('vote-locked')){
var votes = parseInt($(this).parent().find('.votes').text());
if(!$(this).hasClass('vote-off')){
$(this).addClass('vote-off');
$(this).prevAll('a').addClass('vote-locked');
votes--;
}
else{
$(this).removeClass('vote-off');
$(this).prevAll('a').removeClass('vote-locked');
votes++;
}
$(this).parent().find('.votes').text(votes);
}
});
jQuery可以使用addClass和removeClass添加和删除类,
$('#vote-up-%1$d').removeClass('vote-up-on');
$('#vote-up-%1$d').addClass('vote-up-off');
$("#vote-up").click(function () {
$(this).toggle();
$(this).toggleClass("vote vote-down-off");
});
尝试这个
收到Ajax响应时创建回调函数。
function addVoteUpClass() {
if (jQuery(".vote").hasClass("vote-up-on")) {
jQuery(".vote-up-on").removeClass("vote-up-on").addClass("vote-up-off");
}
}
jQuery.ajax({
url: "your_url",
method: "GET",
success: function(data) {
addVoteUpClass();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.