douhuan1380 2016-03-27 06:23
浏览 158

Jquery:Bootstrap表单验证库不处理动态添加的元素

I am using bootstap validator library for form validation.

  //validate when submit button is clicked.
    $('#passenger-form').bootstrapValidator({
     fields: {
       email: {
                    group: '.col-xs-6',
                    validators: {
                        notEmpty: {
                            message: 'Email required'
                        }

                    }
                }
             //rest of the rules here
     }
     });

Sometime my form is dynamically added (based on some conditions made by the user) and at that time validation does not work.

When the form is dynamically added the validation rules are not working. I know jquery.on event can be used to solve this issue, but how can I use it in my case?

  • 写回答

2条回答 默认 最新

  • dtyrxmoj20617 2016-03-27 06:39
    关注

    jQuery.on may not help you in this context, since you are using a plugin.

    Looking at the website for BootrapValidator, it looks like it has been superseded by FormValidation.

    Putting that aside, it looks like your best bet would be to create a function that binds the validator, and call that function when you add form elements dynamically.

    For example:

    function bindForm() {
        //validate when submit button is clicked.
        $('#passenger-form').bootstrapValidator({
            fields: {
                email: {
                    group: '.col-xs-6',
                    validators: {
                        notEmpty: {
                            message: 'Email required'
                        }
                    }
                }
                //rest of the rules here
            }
         });
    }
    //call it by default if the form is present
    bindForm();
    
    //call it when a user generates an event
    function userGeneratedEvent() {
        //do whatever it is that this event is for
        //add the form as described in the question
        bindForm();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题