如何解决提交表单时,使SweetAlert可见
我目前正在Flask项目中工作,我需要发布一个表单,并在表单发布后立即显示SweetAlert。但是问题很快就触发了swal方法,发生了Post方法并返回到使用Flask设置的同一页面。我想在SweetAlert显示并且用户单击“确定”按钮后刷新页面。
这是我的代码:
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")
我只想在用户单击甜蜜警报中的“确定”后触发事件发布。
解决方法
尝试以下解决方案:
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",title: "...",showConfirmButton: false,timer: 1500
});
} else {
swal({
title: "Erreur!",text:
"An error has occurred",type: "error",confirmButtonText: "ok"
});
}
});
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。