簡體   English   中英

消息閃爍而不重新加載頁面?

[英]Message flashing without reloading page?

因此,我有這個非常基本的代碼,它需要一些輸入文本,並在單擊提交按鈕后立即顯示一條消息。 我的問題是,單擊該按鈕后,輸入的任何文本都會消失,因為發布過程會重新加載html模板。 有辦法避免這種情況嗎? 謝謝,下面是我的代碼:

class View(flask.views.MethodView):
    def get(self):
        return flask.render_template('index.html')

    def post(self):
        flask.flash(flask.request.form['expression']) #reprint the text input
        flask.flash(Markup('<br /><h2> Score = '+str(5)+' %</h2>'))
        return self.get() 

(我知道)動態更改DOM的唯一方法是通過AJAX。

http://flask.pocoo.org/docs/patterns/jquery/

有一個很好的例子。

基本上,創建您自己的api。

擁有你的索引

 @app.route('/')
 def index():
      return render_template('index.html')

調用視圖API

@app.route('/test')
def post(object):
    getIt= request.args.listvalues()[0]
    More code...
    result=Markup('<br /><h2> Score = '+str(5)+' %</h2>')
    return jsonify(result)

在您的HTML中,類似於(這不是真實的代碼)

<script>
var $whateves= $("#whateves").whateves({
$.getJSON($SCRIPT_ROOT + '/test', {
 do something?
  }, function(data) {
    $("#result").text(data.result);
  });

顯示新結果

 <span id=result>No message to flash</span>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM