简体   繁体   English

为什么使用javascript时此警报不起作用?

[英]Why this alert doesn't work using javascript?

I'm working on ASP.NET and on the view I have a code like this, If I use the sweet alert outside the function it works, and If I change the sweet alert in to a regular alert it works too but It doesn't work like the code below. 我在ASP.NET上工作,并且在视图上有这样的代码,如果我在功能之外使用了甜蜜警报,并且如果将甜蜜警报更改为常规警报,它也可以工作,但它没有就像下面的代码一样工作。 (I also have the script with the library on the view) (我在视图中也有带有库的脚本)

 <input type="submit" value="Create" class="btn btn-default" onclick="return foo();" /> <script type="text/javascript"> function foo() { swal("Good job!", "You clicked the button!", "success") return true; } </script> 

ok, you forgot to include library js and css 好的,您忘记了包含库js和CSS

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script> <input type="submit" value="Create" class="btn btn-default" onclick="return foo();" /> <script type="text/javascript"> function foo() { swal("Good job!", "You clicked the button!", "success") return true; } </script> 

When you click the submit button, it is submitting a form. 当您单击提交按钮时,它正在提交一个表单。

With an alert, it blocks the browser so the code will wait until you push okay and than submit the form. 有了警报,它将阻止浏览器,因此代码将一直等到您按下“确定”并提交表单。 But the issue when you move to use sweet alert, you can not block the browser action. 但是当您使用甜蜜警报时,您无法阻止浏览器操作。 So you will have to cancel the click and submit the form manually. 因此,您将必须取消点击并手动提交表单。

 // called onclick of the submit button function foo() { swal("foo") .then(function() { // manually submit the form document.getElementById("yourFormId").submit() }); return false; // cancel the button click } 
 <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script> <form id="yourFormId"> <input type="submit" value="Create" class="btn btn-default" onclick="return foo();" /> </form> 

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

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