-1

我有一个具有addmore功能的表单。单击 addmore 时,一组 html 输入字段会附加到表单中,如果用户想要删除添加的行,他/她也可以这样做。一切正常,只是面临删除附加 html 后,附加字段的验证仍然有效的问题。

我正在使用带有引导程序的ValidaionsJs进行验证。

下面是我得到的错误

类型错误:$message 未定义

$allErrors = $message.find('.' + this.options.err.clazz.split(' ').join('.') +...

在此处输入图像描述

     var $parent      = $field.closest(row),
            $message     = $field.data(ns + '.messages'),
            $allErrors   = $message.find('.' + this.options.err.clazz.split(' ').join('.') + '[data-' + ns + '-validator][data-' + ns + '-for="' + field + '"]'),
            $errors      = validatorName ? $allErrors.filter('[data-' + ns + '-validator="' + validatorName + '"]') : $allErrors,
            $icon        = $field.data(ns + '.icon'),
            // Support backward
            container    = ('function' === typeof (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container))
                            ? (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container).call(this, $field, this)
                            : (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container),
            isValidField = null;

这是我的html

<fieldset class="col-md-6">
    <a href="javascript:void(0)" class="remove-addmore high-education  fa fa-trash-o" alt="Remove" title="Remove" onclick="removeAddmore(this)"></a>
    <div class="ph10"><legend>Higher Education</legend> </div>
    <div class="form-group ph10 mt10">
        <label for="">Title</label>
        <?php echo $this->Form->input('DoctorEducation.' . $count . '.title', array('placeholder' => 'ex. DM or Diploma', 'label' => false, 'div' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'))); ?>
    </div>
    <?php echo $this->Form->input('DoctorEducation.' . $count . '.id');
    echo $this->Form->hidden('DoctorEducation.' . $count . '.user_id', array('value' => $userID));
    ?>
    <div class="form-group ph10 mt10">
        <?php
        $data = array(3);
        $GraduationDegries = array();
        foreach ($data as $idValue) {
            $GraduationDegries[$idValue] = $degreeTypes[$idValue];
        }
        $degreeTypes = $GraduationDegries;

        echo $this->Form->input('DoctorEducation.' . $count . '.degree_type_id', array('empty' => 'Select Course', 'label' => 'Course', 'div' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'), 'options' => $degreeTypes));
        ?>
    </div>
    <div class="form-group ph10 mt10">
<?php echo $this->Form->input('DoctorEducation.' . $count . '.college_hospital', array('label' => 'College / Hospital Name', 'div' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'))); ?>
    </div>
    <div class="form-group">
        <label for="" class="ph10">Duration</label>
        <div class="clearfix">
            <div class="col-lg-6">
                <div class="input-group year">
<?php echo $this->Form->input('DoctorEducation.' . $count . '.start_date', array('placeholder' => 'Start Year', 'type' => 'text', 'label' => false, 'div' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'), 'div' => false)); ?>
                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>
            <div class="col-lg-6">
                <div class="input-group year">
<?php echo $this->Form->input('DoctorEducation.' . $count . '.end_date', array('placeholder' => 'End Year', 'div' => false, 'type' => 'text', 'label' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'), 'div' => false)); ?>
                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>
        </div>
    </div>
</fieldset>

这是我用来删除 HTML 的 JS

function removeAddmore(_this) {
    //  $(_this).closest('fieldset').remove();

    var classname = $(_this).attr('class');
    if (classname.indexOf("post-education") >= 0) {
        console.log($("#higher_edu_count").val());
        var val = $("#higher_edu_count").val();
        $("#higher_edu_count").val(val - 1);
        $(_this).closest('fieldset').remove();
    } else if (classname.indexOf("past-exprience") >= 0) {
        var val = $("#higher_edu_count").val();
        $("#higher_edu_count").val(val - 1);
        $(_this).closest('fieldset').remove();
    } else if (classname.indexOf("high-education") >= 0) {
        var val = $("#higher_edu_count").val();
        $("#higher_edu_count").val(val - 1);
        $(_this).closest('fieldset').remove();
    }
}

删除 HTML 后有什么方法可以停止验证。

请让我知道有什么我必须提及的以进行更多说明

有一些有用的链接。

http://formvalidation.io/examples/switching-validators-same-field/
http://formvalidation.io/examples/adding-dynamic-field/
http://formvalidation.io/examples/ignoring-validation/

更新

尝试以下代码但没有成功

 function removeAddmore(_this) {
    //  $(_this).closest('fieldset').remove();

    var classname = $(_this).attr('class');
    if (classname.indexOf("post-education") >= 0) {
        var val = $("#higher_edu_count").val();
        $("#higher_edu_count").val(val - 1);
        //   $(_this).closest('fieldset').remove();
        $('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][title]', false);
        $('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][degree_type_id]', false);
        $('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][college_hospital]', false);
        $('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][start_date]', false);
        $('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][end_date]', false);
    } else if (classname.indexOf("past-exprience") >= 0) {
        var val = $("#higher_edu_count").val();
        $("#higher_edu_count").val(val - 1);
        $(_this).closest('fieldset').remove();
    } else if (classname.indexOf("high-education") >= 0) {
        var val = $("#higher_edu_count").val();
        $("#higher_edu_count").val(val - 1);
        $(_this).closest('fieldset').remove();
    }

}
4

2 回答 2

0

我解决了你的问题并尝试了这个http://formvalidation.io/examples/adding-dynamic-field/。这会很有帮助。使用这个 Javascript 代码

<script>
    jQuery(document).ready(function ($) {
        $('#education')
                // Remove button click handler
                .on('click', '.remove-addmore', function () {

                    var index = $(this).attr('id');
                    var $row = $(this).parent('fieldset');

                    $('#education')
                            .formValidation('removeField', $row.find('[name="data[DoctorEducation][' + index + '][title]"]'))
                            .formValidation('removeField', $row.find('[name="data[DoctorEducation][' + index + '][degree_type_id]"]'))
                            .formValidation('removeField', $row.find('[name="data[DoctorEducation][' + index + '][college_hospital]"]'))
                            .formValidation('removeField', $row.find('[name="data[DoctorEducation][' + index + '][start_date]"]'))
                            .formValidation('removeField', $row.find('[name="data[DoctorEducation][' + index + '][end_date]"]'));
                    // Remove element containing the option
                    $(this).parent('fieldset').remove();

                    // Remove field

                });
    });
</script>
于 2015-08-25T19:52:10.593 回答
0

大多数情况下,谷歌浏览器将数据存储为 cookie,只要存储这些值,这些值就会给出相同的结果。请尝试在多个浏览器中测试或清除历史记录并运行文件。

于 2015-08-24T10:42:11.237 回答