1

我的目标是仅在用户单击 SweetAlert 框上的“确定”后才允许提交表单。

所以,他点击一张图片(表格),出现带有一些说明的框,他阅读并点击“确定”,然后提交表格。

这是表单代码:

<form id="formpag" action="https://example.com/request.html" method="post">
<input type="hidden" name="code" value="7055C88A445678A00461CFA120F4A2E7" />
<input type="image" src="https://example.com/buttons/subscriptions/120x53-subscribe.gif" name="submit" alt="Pay with Example.com - it is fast and reliable!" />
</form>

现在是 jQuery/Sweet Alert 部分:

jQuery( document ).ready(function( $ ) {

 $(function() {
   $('#formpag').submit(function() {

   swal({   title: "Be Advised",
            text: "You need to check your email account after payment.",
            imageUrl: "images/thumbs-up.jpg" });        
          });
      });
   });

问题:就像现在一样,框正在向用户显示,但几秒钟后,表单正在提交,即使他没有点击 SweetAlert 框上的 OK 按钮。

只有当他单击“确定”时才必须提交表格。

任何帮助将非常感激。(对不起,我知道这是一个愚蠢的问题,但 SweetAlert 文档并没有帮助我)。

4

2 回答 2

0

您需要防止默认处理。将您的功能更新为以下

 $('#formpag').submit(function(event) {
    event.preventDefault();
    swal({   title: "Be Advised",
            text: "You need to check your email account after payment.",
            imageUrl: "images/thumbs-up.jpg" });        
     });
 });

供参考 - http://api.jquery.com/event.preventdefault/

于 2015-08-01T13:30:43.273 回答
0

我已经做了。我找到了另一个 SweetAlert 代码,更适合我的需要,我可以将它混合到一个 jquery 基本函数中。

这是最终结果:

jQuery( document ).ready(function( $ ) {

$(function() {
  $('#formpag').submit(function() {
    event.preventDefault();

  swal({
    title: "Be advised",
    text: "You need to check your email account after payment.",
    type: "warning",
    showCancelButton: false,
    confirmButtonColor: 'blue',
    confirmButtonText: 'Ok, got it.',
    closeOnConfirm: false,

  },
  function(){
     $('#formpag').submit();
   });  
  });
 });
});
于 2015-08-02T14:38:50.167 回答