删除html后,验证工作正常

时间:2015-08-24 09:44:19

标签: javascript php jquery twitter-bootstrap-3 formvalidation-plugin

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

  

我正在使用ValidaionsJs和bootstrap进行验证。

以下是我得到的错误

  

TypeError:$ message未定义

     

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

enter image description here

     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();
    }

}

2 个答案:

答案 0 :(得分:0)

大部分时间谷歌浏览器将数据存储为cookie,这些值只要存储就会得到相同的结果。请尝试在多个浏览器中测试或清除历史记录并运行该文件。

答案 1 :(得分: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>