Javascript如何在单击时自动更改word而无需刷新浏览器。?
我在这里迷路了,对javascript不是很精通。我想在用户单击喜欢或不喜欢时显示“感谢投票”,并且“感谢投票”一词会自动显示,无需刷新页面 这是我的html:Javascript如何在单击时自动更改word而无需刷新浏览器。?,javascript,jquery,html,django,twitter-bootstrap,Javascript,Jquery,Html,Django,Twitter Bootstrap,我在这里迷路了,对javascript不是很精通。我想在用户单击喜欢或不喜欢时显示“感谢投票”,并且“感谢投票”一词会自动显示,无需刷新页面 这是我的html: {% if poll.privacy == "own" and request.user.get_profile.parliment != poll.location %} You do not have permission to vote this. {% else %} {% if has_vote %} {% if poll.r
{% if poll.privacy == "own" and request.user.get_profile.parliment != poll.location %}
You do not have permission to vote this.
{% else %}
{% if has_vote %}
{% if poll.rating_option == '1to5' %}
<div class="rate">
<div id="poll-rate-{{ poll.pk }}"></div>
</div>
{% else %}
Thanks for your vote.
{% endif %}
{% else %}
{% if poll.rating_option == 'yes_no' %}
<a href="javascript:void(0)" class="rate btn btn-xs btn-success mr5 vote-positive" rel="{% url 'vote_vote' poll.pk 1 %}" alt="{{ poll.pk }}">Yes</a>
<a href="javascript:void(0)" class="rate btn btn-xs btn-danger vote-negative" rel="{% url 'vote_vote' poll.pk 0 %}" alt="{{ poll.pk }}">No</a>
{% elif poll.rating_option == 'like_dislike' %}
<a href="javascript:void(0)" class="rate btn btn-xs btn-success mr5 vote-positive" rel="{% url 'vote_vote' poll.pk 1 %}" alt="{{ poll.pk }}">Like</a>
<a href="javascript:void(0)" class="rate btn btn-xs btn-danger vote-negative" rel="{% url 'vote_vote' poll.pk 0 %}" alt="{{ poll.pk }}">Dislike</a>
{% elif poll.rating_option == '1to5' %}
<div class="rate">
<div id="poll-rate-{{ poll.pk }}"></div>
</div>
{% endif %}
{% endif %}
{% endif %}
{%if poll.privacy==“own”and request.user.get_profile.parliment!=poll.location%}
你没有投票权。
{%else%}
{%if拥有_投票权%}
{%if poll.rating_option='1到5%}
{%else%}
谢谢你的投票。
{%endif%}
{%else%}
{%if poll.rating_option='yes_no%}
{%elif poll.rating_option=='像“%”
{%elif poll.rating_option='1到5%}
{%endif%}
{%endif%}
{%endif%}
下面是我的javascript:
function bindVoteHandler() {
$('a.vote-positive, a.vote-negative').click(function(event) {
event.preventDefault();
var link = $(this).attr('rel');
var poll_pk = $(this).attr('alt');
var selected_div = $(this).parent('div');
selected_div.html('<img src="{{ STATIC_URL }}img/loading_small.gif" />');
$.ajax(link).done(function( data ) {
var result_div = $('div#vote-result-'+poll_pk);
result_div.html(data);
result_div.removeClass('vote-result-grey-out');
selected_div.html('<small>Thanks for your vote.</small>');
});
});
};
$(document).ready(function() {
bindPostCommentHandler();
bindLoadCommentHandler();
bindDeleteCommentHandler();
bindVoteHandler();
$('.show-abuse').click(function(){
var url = $(this).attr('rel');
$('#abuseModal .modal-body').load(url);
});
//load rating html
$('#rate_list').load("{% url 'rate_list' request.session.location_parliment_id %}");
});
</script>
函数bindVoteHandler(){
$('a.vote-positive,a.vote-negative')。单击(函数(事件){
event.preventDefault();
var link=$(this.attr('rel');
var poll_pk=$(this.attr('alt');
var selected_div=$(this.parent('div');
选定的分区html(“”);
$.ajax(link).done(函数(数据){
var result_div=$('div#投票结果-'+投票结果);
result_div.html(数据);
result_div.removeClass(“投票结果灰显”);
选中的_div.html('谢谢您的投票');
});
});
};
$(文档).ready(函数(){
bindPostCommentHandler();
bindLoadCommentHandler();
bindDeleteCommentHandler();
bindVoteHandler();
$('.show滥用')。单击(函数(){
var url=$(this.attr('rel');
$('#abuseModal.modal body').load(url);
});
//负载额定值html
$('rate_list').load(“{%url'rate_list'request.session.location_parliment_id%}”);
});
有人知道我为什么需要在Like/Vote/rate之后刷新页面以使其显示(感谢您的投票)的问题吗?
请有人知道帮助或与我分享链接
下图:
单击“喜欢”之前:
单击“喜欢”后:
然后,当我刷新浏览器时,会显示单词,单击“喜欢”时会自动显示。
您应该使用jQuery实现单击功能。当按下投票按钮时,此脚本将显示以前隐藏的投票确认。如果你需要隐藏投票按钮,我也包括在内。显然,您需要用适当的类名替换类名
<style>
.yourVoteConfirmation {
display: none;
}
</style>
<script>
$(document).ready(function () {
$('.yourVoteButton').click(function() {
$('.yourVoteConfirmation').show('fast');
// if you need to hide the vote button
$('.yourVoteButton').css('display', 'none');
});
});
</script>
.您的确认书{
显示:无;
}
$(文档).ready(函数(){
$('.yourVoteButton')。单击(函数(){
$('.yourVoteConfirmation').show('fast');
//如果你需要隐藏投票按钮
$('.yourVoteButton').css('display','none');
});
});
什么时候调用bindVoteHandler()函数?@Alex,对不起,我没有发布所有javascript代码。(有问题地更新)这里是:$(文档).ready(函数(){bindPostCommentHandler();bindLoadCommentHandler();bindDeleteCommentHandler();bindVoteHandler();$('.show-滥用')。单击(函数(){var-url=$(this.attr('rel');$('.#abuseModal.modal-body'))。加载(url);});也许你应该使用$.ajax({url:…,键入:…})。完成…并尝试一个outhi@Alex,我没有收到任何错误,只是我需要刷新浏览器以显示“感谢”在投票时替换投票符号。你能对此进行更多扩展吗?我真的不明白,还有点不知道Js。。