【问题标题】:Make The SweetAlert Visible when i submit the form当我提交表单时使 SweetAlert 可见
【发布时间】:2020-10-04 17:08:21
【问题描述】:

我目前正在处理一个 Flask 项目,我需要在该项目中发布一个表单,并在表单发布后立即显示一个 SweetAlert。但是问题是,一旦触发了Swal方法,Post方法就会发生并返回到使用Flask设置的同一页面。我想在 SweetAlert 显示并且用户单击 Ok 按钮后刷新页面。

这是我的代码:

 if (k > 0) {
    e.preventDefault();
  } else {
    swal({
      title: "Thank You",
      text: "You Would Be Notified Soon",
      icon: "success",
    });
  }
});

和我的 Python 后端:

if request.method == "POST":
        email = request.form['email']
        link = request.form['link']
        crntprice = int(request.form['crntprice'])
        btn_group = request.form.getlist("btn_group")
        change_price = False
        if "change_in_price" in btn_group:
            change_price = True
        tgt_price = 0
        if "btn_change_price" in btn_group:
            tgt_price = int(request.form['tgt_price'])
        item = Prices(email=email, link=link, crnt_price=crntprice,
                      change_price=change_price, lower_price=tgt_price)
        try:
            db.session.add(item)
            db.session.commit()
            return redirect('/')
        except:
            return "Error"
    else:
        return render_template("index.html")

我只想在用户点击甜蜜警报后触发 Post-event。

【问题讨论】:

    标签: javascript html flask sweetalert


    【解决方案1】:

    试试这个解决方案:

    swal({
                title: "...",
                text: "Are you sure you want to...?",
                type: "success",
                showCancelButton: true,
                confirmButtonText: "Yes",
                cancelButtonText: "No"
            }).then(result => {
                if (
                    result.dismiss !== swal.DismissReason.cancel &&
                    result.dismiss !== swal.DismissReason.backdrop
                ) {
                    return this.genericService
                        .servicePost("/endpoint", payload)
                        .subscribe(data => {
                            if (data) {
                                swal({
                                    position: "center",
                                    type: "success",
                                    title: "...",
                                    showConfirmButton: false,
                                    timer: 1500
                                });
    
                               
                            } else {
                                swal({
                                    title: "Erreur!",
                                    text:
                                        "An error has occurred",
                                    type: "error",
                                    confirmButtonText: "ok"
                                });
                            }
                        });
                }
            });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多