简体   繁体   English

行动与提交表单之间的冲突

[英]Conflict between action and onsubmit of a form

I am developing an application in python Flask. 我正在使用python Flask开发应用程序。 I have a form, on click of which an action is triggered in order to execute a part in app.py. 我有一个表单,单击该表单会触发一个动作,以执行app.py中的零件。 However I want the button to be clickable only once in 24 hours. 但是我希望按钮在24小时内只能单击一次。 So I tried to implement the onsubmit event. 因此,我尝试实现onsubmit事件。 But since the onsubmit gets executed first I have problems with the action part relating to the code in app.py getting executed. 但是由于onsubmit首先被执行,所以我的动作部分与app.py中的代码有关。

1)This is the form code: 1)这是表单代码:

 <form name="Update" action="/update" method="post">
    <input type="submit" id="myBtn" value="Update" onsubmit="myFunction()">
 </form>`

2)The action='/update" triggers this part in app.py: 2)action ='/ update“会在app.py中触发此部分:

@app.route('/update',methods=['POST'])
def Update():
 ....
 ....
 ....
return render_template('index.html', output=output)  

3)The code I tried to enable timeout for the form submit which caused the conflict. 3)我试图为表单提交启用超时的代码,这导致了冲突。

<script>
  function myFunction() 
  {
    document.getElementById("myBtn").disabled = true;
    setTimeout(function()
    {
      document.getElementById("myBtn").disabled = false;
    }, 200000);
  }
</script>

Since document.getElementById("myBtn").disabled = true; 由于document.getElementById(“ myBtn”)。disabled = true; is hitting first the action part is not getting executed. 首先击中动作部分未执行。 Please provide an assistance on how to approach this. 请提供有关如何解决此问题的帮助。 Thanks in advance. 提前致谢。

You may try to place "onsubmit" to form rather than input. 您可以尝试将“提交”形式而不是输入形式。

<form name="Update" action="/update" method="post" onsubmit="myFunction()">
    <input type="submit" id="myBtn" value="Update">
</form>

js part js部分

<script>
  function myFunction() 
  {
      return true; /// form execution goes on
      return false; /// form execution stops
  }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM