twitter-bootstrap BootstrapValidator 表单提交无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23757401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 20:45:03  来源:igfitidea点击:

BootstrapValidator form submit not working

javascriptformstwitter-bootstrapjqbootstrapvalidation

提问by Hacker Rocker

I have a form with 2 buttons. One button submits the form in ajax format and another button submits the form in normal fashion.

我有一个带有 2 个按钮的表单。一个按钮以 ajax 格式提交表单,另一个按钮以正常方式提交表单。

$('#form_createAd').bootstrapValidator({
    submitHandler: function(validator, form, submitButton) {
        if (submitButton.attr('value') == "cart"){
            submit_cartDetails(); //This function submits details thru ajax post
        } else {
            form.submit(); // Also Tried $('#form_createAd').submit()
        }
    }, fields: {
        title: {
            message: 'The username is not valid',
            validators: {
                notEmpty: {
                    message: 'The username is required and can\'t be empty'
                },
                stringLength: {
                    min: 6,
                    max: 30,
                    message: 'The username must be more than 6 and less than 30 characters long'
                },
                regexp: {
                    regexp: /^[a-zA-Z0-9_\.]+$/,
                    message: 'The username can only consist of alphabetical, number, dot and underscore'
                }
            }
        }
    }
});

This Is The HTML Part Of Buttons Inside Form Section

这是表单部分内按钮的 HTML 部分

<button  id="cart" value="cart" class="btn btn-primary" >Add To Cart</button>             
<button type="submit" name="submit" value="proceed" class="btn btn-success">Proceed</button>

The form doesn't submit. What did I do wrong?

表单不提交。我做错了什么?

回答by Phuoc Nguyen

I'm the creator of BootstrapValidator plugin.

我是 BootstrapValidator 插件的创建者。

The issue is caused by using submitto name the submit button. Changing name="submit"to something else will fix the issue.

该问题是由使用submit命名提交按钮引起的。更改name="submit"为其他内容将解决问题。

回答by gmslzr

It could be a handful of things:

这可能是一些事情:

  • form id matches both html and js
  • submitHandler is actually working?
  • is the library even loaded?
  • 表单 id 匹配 html 和 js
  • submitHandler 真的有效吗?
  • 图书馆甚至被加载了吗?

That's just at the top of my head. A fiddlemight help.

那只是在我的头顶。一拨弄可能的帮助。