Javascript 即使在删除html之后,验证仍然有效
我有一个表单,其中有Javascript 即使在删除html之后,验证仍然有效,javascript,php,jquery,twitter-bootstrap-3,formvalidation-plugin,Javascript,Php,Jquery,Twitter Bootstrap 3,Formvalidation Plugin,我有一个表单,其中有addmore功能。单击addmore时,一组html输入字段会附加到表单中,如果用户想要删除添加的行,他/她也可以这样做。所有的工作都很好,只是面临这样一个问题:在删除附加的html之后,附加字段的验证仍然有效 我正在使用bootstrap进行验证 下面是我得到的错误 TypeError:$消息未定义 $allErrors=$message.find('..+this.options.err.clazz.split(' “)。连接(“.”)+ 这是我的html <f
addmore
功能。单击addmore时,一组html输入字段会附加到表单中,如果用户想要删除添加的行,他/她也可以这样做。所有的工作都很好,只是面临这样一个问题:在删除附加的html之后,附加字段的验证仍然有效
我正在使用bootstrap进行验证
下面是我得到的错误
TypeError:$消息未定义
$allErrors=$message.find('..+this.options.err.clazz.split('
“)。连接(“.”)+
这是我的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后停止验证
请让我知道我是否有什么需要进一步澄清的地方
这里有一些有用的链接
更新 尝试以下代码,但没有成功
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();
}
}
大多数情况下,google chrome会将数据存储为Cookie,只要存储这些数据,这些值就会给出相同的结果。请尝试在多个浏览器中进行测试,或清除历史记录并运行该文件。我已经解决了您的问题并尝试了此操作。这将非常有用。使用此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>
jQuery(文档).ready(函数($){
$(“教育”)
//删除按钮单击处理程序
.on('click','remove addmore',函数(){
var index=$(this.attr('id');
var$row=$(this.parent('fieldset');
$(“教育”)
.formValidation('removeField',$row.find('[name=“data[DoctorEducation]['+index+']][title]“]'))
.formValidation('removeField',$row.find('[name=“data[DoctorEducation]['+index+'][degree\u type\u 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]“]);
//删除包含选项的元素
$(this.parent('fieldset').remove();
//删除字段
});
});
如果这是您的代码,您忘了在其前面添加var lol“$allErrors=$message.find('.+this.options.err.clazz.split('').join('.')+…”请检查更新问题$field是否未定义或在.closest()上未返回任何内容,因为我删除了html。请再次转到问题。如果($parent){}…在其他情况下什么也不做?让我试试这个。顺便说一句,如果你检查我的代码,我已经试过了。`$('#教育')。formValidation('enablefieldcalidators','data[DoctorEducation][1][title]',false);`现在试试“removeField”。它可能会对你有帮助!!它的工作很有魅力..谢谢你帮我省下一天的时间。
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();
}
}
<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>