【问题标题】:validations are working even after removing html即使在删除 html 后验证仍然有效
【发布时间】:2015-08-24 09:44:18
【问题描述】:

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

我正在使用 ValidaionsJs 和引导程序进行验证。

以下是我遇到的错误

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

}

【问题讨论】:

  • 如果这是你的代码,你忘了在它前面添加 var 哈哈" $allErrors = $message.find('.' + this.options.err.clazz.split(' ').加入('.') +... "
  • 查看更新问题
  • $field 要么未定义,要么在 .closest() 上不返回任何内容
  • 是的,因为我删除了 html。请再次提问。
  • if( $parent ) { } ... 在其他情况下什么都不做?

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我解决了你的问题并尝试了这个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>
    

    【讨论】:

    • 让我试试这个。顺便说一句,如果您检查我的代码,我已经尝试过了。 ` $('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][title]', false);`
    • 现在试试“removeField”。可能对你有帮助
    • 宾果游戏!它的工作就像魅力..感谢您节省了我的另一天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多