如何在使用 jQuery 的 HTML 表单中使用两个按钮时将数据提交到烧瓶

sudointhehouse

我正在尝试实现一个 Web 应用程序,该应用程序接收用户数据以执行某些操作,然后在任务完成后显示一个新页面。在执行任务时(因为我正在做的事情需要一段时间),我使用 jQuery 向用户显示它正在后台执行,他/她需要等待。

Javascript 的一部分工作正常并显示等待屏幕,但表单数据没有发送到我在 python 上的烧瓶上运行的后端。

我的 HTML 文件login.html

<!doctype html>

<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">  
    
    <title>Connect page</title>

    <!-- Self written CSS  -->
    <link rel="stylesheet" href="static/style.css">
  </head>


<body>
  <div class="container"  id="main">
    <div class="alert" role="alert">
      <h4 class="alert-heading">Enter login details</h4>
    </div>

    <form action="#" method="post" id="form_data">
      <div class="form-group">
        <label>fieldx</label>
        <input type="text" class="form-control" value="abc" name="fieldx" />
      </div>

      <div class="form-group">
        <label>fieldy</label>
        <input type="text" class="form-control" value="xyz" name="fieldy" />
      </div>
    
      <div class="three_buttons">
        <button type="button" class="btn btn-primary btn-lg" name="btn_identifier_login" value="do_a" id="ida">Do a</button>
        <button type="button" class="btn btn-primary btn-lg" name="btn_identifier_login" value="do_b" id="idb">Do b</button>
      </div>
    </form>
  </div>

  <!-- only this to be shown when "Do a" is clicked  -->
  <div class="container hide-before-click" id="sub">
    <div class="alert" role="alert">
      <h4 class="alert-heading">Please wait while we do a...</h4>
    </div>
  </div>

  <!-- only this to be shown when "Do b" is clicked  -->
  <div class="container hide-before-click" id="search_page">
    <div class="alert" role="alert">
      <h4 class="alert-heading">Please wait while we do b...</h4>
    </div>
  </div>

  <script>
    var ida = document.getElementById("ida");
    var sub = document.getElementById("sub");
    var main = document.getElementById("main");
    var idb = document.getElementById('idb');
    var form_data = document.getElementById("form_data");
    var search_page = document.getElementById("search_page");

    ida.addEventListener("click", function(e) {
    main.classList.add("hide-before-click");
    sub.classList.remove("hide-before-click");
    form_data.submit();
    e.preventDefault();
    });

    idb.addEventListener('click', function(e) {
    main.classList.add('hide-before-click');
    search_page.classList.remove('hide-before-click');
    form_data.submit();
    e.preventDefault();
    });
    
  </script>

</body>
</html>

使用flask的python webapp:

from flask import Flask, redirect, render_template, url_for, request, session, send_file
import time
app = Flask(__name__)
app.secret_key = "a key"

@app.route("/", methods = ["POST", "GET"])
def login():
    if request.method == "POST":
        val = request.form.get("btn_identifier_login")
        print(f"{val} is the value recieved from the button") # this is so i can debus and see what is actually recieved.
        #Sadly, this always reads 'None is the value recieved from the button'
        fieldx = request.form.get("fieldx")
        session["fieldx"] = fieldx
        fieldy = request.form.get("fieldy")
        session["fieldy"] = fieldy
        if val == 'do_a':
            time.sleep(3)# actual implementation does something complicated that takes about 3-4 seconds (during which JS shows the please wait text for a)
            return "a done"
        elif val == 'do_b':
            time.sleep(3) # actual implementation does something complicated that takes about 3-4 seconds (during which JS shows the please wait text for b)
            return "b done"
        else:
            time.sleep(1) # this is only here for debugging
            return "didnt understand button value"
    else:
        return render_template("login.html") # this is the main login page

if __name__ == "__main__":
    app.run(debug=True)

css文件以防万一:

@import url('https://fonts.googleapis.com/css?family=Montserrat:400,600');

body {
    margin: 0;
    font-family: 'Montserrat';
    background: #FFFAFA
}

.alert{
    background: #a9a9a9 
}


.container {
    padding-top: 20px;
    text-align: center;
    padding: .8em 1.2em;
}

.container2 {
    padding-top: 20px;
    text-align: center;
    padding: .8em 1.2em;
}

.three_buttons {
    display: flex;
    width: 300px;
    justify-content: space-between;
    padding-top: 20px;
    padding-bottom: 20px;
    text-align: center;
    margin: auto;
}


.hide-before-click {
    display: none;
}

.hide {
    display: none;
    padding: .8em 1.2em;
    padding-top: 20px;
    text-align: center;
}

.spinner-border {
    text-align: center;
}

.submit_button {
    width: calc(100% - 3em);
    margin: auto;
    padding-top: 50px;
}

.form-group {
    padding-top: 10px;
    text-align: left;
}

注意:这是我第一次做任何与 webapp 相关的事情。

sudointhehouse

这就是我最终解决我的问题的方法。希望对以后的新手有所帮助。

仅对login.html文件进行了更改。

div方法是通过按钮变为这样:

<div class="three_buttons">
        <button type="submit" class="btn btn-primary btn-lg" name="btn_identifier_login" value="do_a" id="ida">Do a</button>
        <button type="submit" class="btn btn-primary btn-lg" name="btn_identifier_login" value="do_b" id="idb">Do b</button>
</div>

并且 Javascript 更改为以下内容:

<script>
    var ida = document.getElementById("ida");
    var sub = document.getElementById("sub");
    var main = document.getElementById("main");
    var idb = document.getElementById("idb");
    var form_data = document.getElementById("form_data");
    var search = document.getElementById("search");

    ida.addEventListener("click", changedisp_sub);
    idb.addEventListener("click", changedisp_search);

    function changedisp_sub() {
      main.classList.add("hide-before-click");
      sub.classList.remove("hide-before-click");  
      }

    function changedisp_search() {
      main.classList.add("hide-before-click");
      search.classList.remove("hide-before-click");  
      }
    
</script>

非常适合我。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Jquery AJAX提交HTML表单

如何在HTML模式下使用JQuery InputMask

如何在使用javascript或jQuery刷新时在html视图页面中显示LocalStorage数据?

javascript-如何在jquery中使用Ajax从Json发送数据到html元素?

如何使用jQuery附加HTML

如何在ASP.NET MVC中使用jQuery将varbinary图像显示到html表?

如何在滚动上使用jQuery动画HTML元素

如何在HTML 5验证之前使用jquery验证表单

npm安装后如何在html中使用jQuery?

如何在HTML标记中使用JQuery设置变量

如何在jQuery Autocomplete中使用HTML源代码?

使用JQuery Ajax将表单提交到servlet

如何使用jQuery附加HTML?

如何在jquery函数中使用smarty vars {html}输出

启用单选按钮时如何在html中使用jquery显示消息

HTML-如何在使用jQuery Mobile CSS时做一个方形的普通按钮

不能使两个按钮在带有HTML的jQuery中使用相同的功能

如何将表单数据从jQuery提交到PHP?

如何使用Html()jQuery

如何在jquery的.html()中添加两个变量

如何在Phantomjs中使用jQuery选择html元素?

使用jQuery将数据从HTML发布到烧瓶视图

如何使用jquery验证插件ajax不使用现有电子邮件将表单提交到数据库

是否可以使用两个按钮将 GET 表单提交到两个不同的视图?

使用jQuery点击回车时提交html表单

在jquery中使用.html(value To show)时如何在td元素中添加span元素

如何在 jQuery 中使用 HTML 模板标签?

尝试使用 jQuery 使用 JSON 数据填充两个链接的 html 下拉对象时出错?

如何在动态创建的表单中使用 JQuery Ajax 将表单数据提交到 ASP.NET Core MVC 操作