繁体   English   中英

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

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

我在引导程序4中有一个预览按钮。如果表单无效,我想显示验证错误,如果表单有效,我想显示模式窗口。

我有这个代码。

    $("#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

如果表单无效,则会收到验证错误。

如果取消注释第二行以显示模态,则会得到模态窗口。

但是,如果验证中没有错误,则模式窗口在else不起作用。

如果验证中没有错误,则alert (以上显示模式)将在else情况下工作。

如果没有验证错误,如何显示模式窗口?

在jQuery中显示元素的一种简单方法是使用.show()方法

例如:

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

在这里您可以找到有关.show()的文档http://api.jquery.com/show/

希望能帮助到你

我会检查您的表单变量的值。 如果它在位置1而不是在else上起作用,则使我认为checkValidity()返回true或if语句引发异常。

也许加一个; 到您的var form

暂无
暂无

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

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