在动态添加多个字段时进行Bootstrap验证 [英] Bootstrap validation on dynamically adding multiple fields

查看:78
本文介绍了在动态添加多个字段时进行Bootstrap验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用bootstrap v3.1.1,我想通过bootstrap验证来验证表单,但是谁包含一个用于克隆3个字段的按钮.通过克隆,一切工作都很好,但是我无法验证克隆的字段.这是我的HTML表单:

I am using bootstrap v3.1.1 and I want to validate a form with bootstrap validation but who contains a button to clone 3 fields. With cloning everything works nice, but I can't validate the cloned fields. Here is my HTML Form:

<form id="myForm" action="myAction">
    <div class="row" id="line_1">
        <div class="col-md-2 form-group">
            <input type="text" class="form-control input-sm" id="idFirstField_1" name="firstField[]">
        </div>
        <div class="col-md-2 form-group">
            <input type="text" class="form-control input-sm" id="idSecondField_1" name="secondField[]">
        </div>
        <div class="col-md-2 form-group">
            <input type="text" class="form-control input-sm" id="idThirdField_1" name="thirdField[]">
        </div>
    </div>
    <a id="cloneButton">add line</a>
</form>

在我拥有的JavaScript文件中:

In JavaScript file I Have:

    $(document).ready(function () {
    var count = 2;
    $('#cloneButton').click(function () {
        var klon = $('#line_1');
        klon.clone().attr('id', 'line_' + (++count)).insertAfter($('#line_1'));
        $('#line_' + count).children('div').children('input').each(function () {
            $(this).val('');
            var oldId = $(this).attr('id').split('_');
            $(this).attr('id', oldId[0] + '_' + count);
        });
    });

    //For validating the fields: 
    $('#myForm').bootstrapValidator({
        fields: {
            'firstField[]': {
                validators: {
                    notEmpty: {
                        message: 'Enter a value'
                    }
                }
            },
            'secondField[]': {
                validators: {
                    notEmpty: {
                        message: 'Enter a value'
                    }
                }
            },
            'thirdField[]': {
                validators: {
                    notEmpty: {
                        message: 'Enter a value'
                    }
                }
            }
        }
    });
});

我现在必须使用这样的东西

I now that I must to use somethings like this

$('#myForm').bootstrapValidator('addField', klon.find('[name="firstField[]"]'));

对于每个字段,但我现在不知道如何正确执行.请帮我.谢谢!

for each field, but I don't now how correctly do it. Please help me. Thanks!!

推荐答案

如果在input循环中插入方法addField,它应该可以工作.我还建议将您的第一个row保存为模板.

if you insert the method addField in your input loop, it should work. I also suggest to save your first row as a template.

var template = $('#line_1').clone();
var options = {
    fields: {
        'firstField[]': {
            validators: {
                notEmpty: {
                    message: 'Enter a value 1'
                }
            }
        },
        'secondField[]': {
            validators: {
                notEmpty: {
                    message: 'Enter a value 2'
                }
            }
        },
        'thirdField[]': {
            validators: {
                notEmpty: {
                    message: 'Enter a value 3'
                }
            }
        }
    }
};
$('#myForm').bootstrapValidator(options);

$('#cloneButton').click(function () {
    var rowId = $('.row').length + 1;
    var validator = $('#myForm').data('bootstrapValidator');
    var klon = template.clone();          
    klon.attr('id', 'line_' + rowId)
        .insertAfter($('.row').last())
        .find('input')
        .each(function () {
            $(this).attr('id', $(this).attr('id').replace(/_(\d*)$/, "_"+rowId));
            validator.addField($(this));
        })                   
});

这篇关于在动态添加多个字段时进行Bootstrap验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆