You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap Validator的success函数无法触发,表单提交无法阻止求助

Fixing Bootstrap Validator Success Event & Form Submission Issue

Hey there, I spot two key issues in your code that are causing the success function not to fire and preventing e.preventDefault() from working as expected:

1. Missing fields Wrapper for Validation Rules

Bootstrap Validator requires all field-specific validation rules to be nested inside a fields object in the configuration. Right now, you've placed editPapSub and editPapAcc directly at the root level of the bootstrapValidator options, so the plugin isn't recognizing these validation rules properly. Even if you see some validation feedback, it's likely not the plugin handling it correctly—which is why the success event never triggers.

2. Corrected Code Implementation

Here's the fixed version of your code with the fields wrapper added, plus a couple of minor cleanup tweaks:

$('#editVolForm').bootstrapValidator({ 
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: { // This wrapper was missing!
        editPapSub: {
            validators: {
                stringLength: {
                    min: 1
                },
                regexp: {
                    regexp: /^[0-9]+$/,
                    message: "'Submitted Papers' can only consist of numeric characters."
                },
                notEmpty: {
                    message: "A valid number of 'Papers Submitted' is required." // Fixed typo here
                }
            }
        },
        editPapAcc: {
            validators: {
                stringLength: {
                    min: 1
                },
                regexp: {
                    regexp: /^[0-9]+$/,
                    message: "'Accepted Papers' can only consist of numeric characters."
                },
                notEmpty: {
                    message: "A valid number of 'Papers Accepted' is required."
                }
            }
        }
    }
}).on('success.form.bv', function(e) {
    e.preventDefault(); // This will now work once the plugin recognizes valid fields
    console.log("test"); // You should see this in the console now when validation passes
});

Additional Notes

  • I removed the trailing comma in the stringLength configurations (like min: 1, → min: 1) to avoid potential syntax issues in older browsers.
  • Fixed a small typo in your error message ("Papers Submmited" → "Papers Submitted") for better user clarity.

Once you update your code with the fields wrapper, the Bootstrap Validator will properly process the validation rules, trigger the success.form.bv event when all fields are valid, and e.preventDefault() will successfully block the default form submission.

内容的提问来源于stack exchange,提问作者user3620865

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:49:44