第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

用戶(hù)喜歡帖子時(shí)如何動(dòng)態(tài)更新Flask模板?

用戶(hù)喜歡帖子時(shí)如何動(dòng)態(tài)更新Flask模板?

犯罪嫌疑人X 2021-03-29 19:10:26
我有一個(gè)簡(jiǎn)單的基于博客的網(wǎng)頁(yè),您可以在其中喜歡帖子,但是當(dāng)like_action發(fā)生時(shí),它會(huì)刷新整個(gè)頁(yè)面,當(dāng)您已經(jīng)在頁(yè)面底部時(shí),這確實(shí)很糟糕。所以基本上我只想刷新html的一部分。我讀了很多有關(guān)ajax的文章,但是當(dāng)嘗試實(shí)現(xiàn)它時(shí),效果不是很好:(@posts.route('/like/<int:post_id>/<action>', methods=['GET', 'POST'])@login_requireddef like_action(post_id, action):    post = Post.query.filter_by(id=post_id).first_or_404()    if action == 'like':        current_user.like_post(post)        db.session.commit()    if action == 'unlike':        current_user.unlike_post(post)        db.session.commit()    return render_template('section.html', post = post)<div class="likes" id="result{{post.id}}">  <hr>  {% if current_user.is_authenticated %}    {% if current_user.has_liked_post(post) %}      <a class="updateButton" post_id="{{post.id}}" href="{{ url_for('posts.like_action', post_id=post.id, action='unlike') }}"><img src="{{url_for('static', filename='heart-filled.png')}}" style="width:5%; height:5%;"></a>    {% else %}      <a class="updateButton" post_id="{{post.id}}" href="{{ url_for('posts.like_action', post_id=post.id, action='like') }}"><img src="{{url_for('static', filename='heart-no-fill.png')}}" style="width:5%; height:5%;"></a>    {% endif %}  {% else %}    <img src="{{url_for('static', filename='heart-no-fill.png')}}" style="width:5%; height:5%;">  {% endif %}  <small>{{post.likes.count()}}</small></div>      $(document).ready(function(){        $(document).on('click','.updateButton', function(){          var post_id = $(this).attr('post_id');          req = $.ajax({            url:'/like/<int:post_id>/<action>'            type:'POST'            data: {id:post_id}          });          req.done(function(data){            $('result'+post_id).html(data);          });        });
查看完整描述

2 回答

?
慕少森

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊

在ajax.done()函數(shù)中,您應(yīng)該更改以下內(nèi)容:

$('result'+post_id).html(data)

到這個(gè)

$('#result'+post_id).html(data)

因?yàn)樵趈Query中,您必須添加#到第一個(gè)查詢(xún)中id


查看完整回答
反對(duì) 回復(fù) 2021-04-29
  • 2 回答
  • 0 關(guān)注
  • 346 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)