防止使用SweetAlert提交表单

时间:2015-08-01 13:29:15

标签: javascript jquery forms sweetalert

我的目标是在用户点击SweetAlert盒子上的“Ok”后才允许表单提交。

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

以下是表单代码:

<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" });        
          });
      });
   });

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

只有点击“确定”才能提交表格。

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

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分:0)

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

以下是最终结果:

shell=true

});

}); });

相关问题