Php Jquery:引导表单验证库无法处理动态添加的元素
我正在使用bootstap验证程序库进行表单验证Php Jquery:引导表单验证库无法处理动态添加的元素,php,jquery,twitter-bootstrap,validation,Php,Jquery,Twitter Bootstrap,Validation,我正在使用bootstap验证程序库进行表单验证 //validate when submit button is clicked. $('#passenger-form').bootstrapValidator({ fields: { email: { group: '.col-xs-6', validators: { notEmp
//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
}
});
有时,我的表单会被动态添加(基于用户所做的某些条件),此时验证不起作用
动态添加表单时,验证规则不起作用。
我知道
jquery.on
事件可以用来解决这个问题,但我如何在我的案例中使用它呢 jQuery.on在这种情况下可能对您没有帮助,因为您使用的是插件
查看BootrapValidator的网站,它似乎已被取代
撇开这一点不谈,看起来最好的办法是创建一个绑定验证器的函数,并在动态添加表单元素时调用该函数
例如:
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();
}
您提供的链接中没有可用的文档,我认为插件已关闭。
好的,这就是想法。由于动态添加的元素在应用插件后进入DOM,因此插件没有这些元素的线索。添加新的动态元素时,需要重新应用验证插件。或者更好的做法是不应用插件,而是在表单提交时捕获表单提交事件,然后应用插件。这必须更简单,没有更多的开销。如果您可以共享指向库文档的链接,我可以帮助您。这是库,升级库可能会为您提供所需内容@BlessanKurien是的,添加了示例。