简体   繁体   English

Bootstrap Modal在验证后不起作用,但是警报起作用

[英]Bootstrap Modal not working after validaion, but alert works

I have a preview button in bootstrap 4. I want to show validation error if the form is not valid and show a modal window if the form is valid. 我在引导程序4中有一个预览按钮。如果表单无效,我想显示验证错误,如果表单有效,我想显示模式窗口。

I have this code. 我有这个代码。

    $("#previewBtn").click(function(event) {
    //$("#exampleModal").modal("show");  //Works fine if I uncomment

    //Fetch form to apply custom Bootstrap validation
    var form = $("#order-form")
    //alert(form.prop('id')) //test to ensure calling form correctly

    if (form[0].checkValidity() === false) {
      event.preventDefault();
      event.stopPropagation();
    } else {
        //alert("Validated!");
        $("#exampleModal").modal("show");   
    }
    form.addClass('was-validated'); 
  }) //validation end

I get validation error if the form is not valid. 如果表单无效,则会收到验证错误。

I get the modal window if I uncomment the second line to show the modal. 如果取消注释第二行以显示模态,则会得到模态窗口。

But the modal window is not working in the else if there is no errors in the validation. 但是,如果验证中没有错误,则模式窗口在else不起作用。

The alert (above show modal) is working in the else if there is no errors in the validation. 如果验证中没有错误,则alert (以上显示模式)将在else情况下工作。

How can I show the modal window if there is no validation errors? 如果没有验证错误,如何显示模式窗口?

in jQuery a simple way to show an element is to use .show() method 在jQuery中显示元素的一种简单方法是使用.show()方法

ex: 例如:

$("#exampleModal").show();

here you can find documentation about .show() http://api.jquery.com/show/ 在这里您可以找到有关.show()的文档http://api.jquery.com/show/

hope it helps 希望能帮助到你

I'd check the value of your form variable. 我会检查您的表单变量的值。 if it works in position 1 and not in the else then it leads me to think checkValidity() is either returning true or the if statement is throwing an exception. 如果它在位置1而不是在else上起作用,则使我认为checkValidity()返回true或if语句引发异常。

And maybe add a ; 也许加一个; to your var form line 到您的var form

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

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