动态中的 BootstrapValidator ResetForm
BootstrapValidator ResetForm in Dynamically
我在不同的动态生成 (jQuery) "dialogues" 中包括(多个实例)用于输入域名或 IP 地址的表单。第一个触发验证器,但后续的似乎没有。验证器设置大致如下所示:
function domainNamesPage(e){
getHTML('spf-tool', 'spf4');
nextButton.innerHTML = '<button class="btn">Next</button>';
$( ".form_blocks:last-child" ).after(addButton);
$( "#domainName" ).after(nextButton);
$('#domainName').formValidation({
framework: 'bootstrap',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
server: {
validators: {
callback: {
message: 'The address is not valid',
callback: function(value, validator, $field) {
if ( pass !== me ) {
// error_object
}
return true;
}
}
}
}
}
});
}
在第一个对话中,字段验证器按预期工作,但在随后的 "dialogues" 中似乎没有任何响应。
检查 form
元素本身:
$("form").each(function () {
console.log(this);
});
第一个看起来像这样:
<form id="domainName" class="dmarc_form" autocomplete="on" action="" method="">
后续像这样:
<form id="domainName" class="dmarc_form fv-form fv-form-bootstrap" autocomplete="on" action="" method="" novalidate="novalidate">
按此顺序加载的依赖项:
https://ajax.googleapis.com/ajax/libs/jquery/1.12.1/jquery.min.js
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css
https://formvalidation.io/vendor/formvalidation/css/formValidation.min.css
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
https://formvalidation.io/vendor/formvalidation/js/formValidation.min.js
https://formvalidation.io/vendor/formvalidation/js/framework/bootstrap.min.js
https://use.fontawesome.com/b66d6d44ad.js
https://cdn.fontawesome.com:443/js/stats.js
https://use.fontawesome.com/b66d6d44ad.css" media="all
而 HTML 基本上是:
<form id="domainName" autocomplete="on" action="" method="">
<div id="formGroup" class="form-group has-feedback">
<div class="input_validate_box">
<input class="form-control input_text bold" name="server" placeholder="Domain Name or IP 1" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAwZJREFUWAntVktoE2EQntlNugkltYoKgtDYrMdasQpFPOjBS7WmisWDoOBFEBE8Ciql2IsXEREEwYPWi4U+RMV6aY+tGkWK0NS0RjQ+KsVIbdrS7P5+G7qb3WRTY00EsT8k/7x2vsm8skQrZyUD/3sG2EjAI1Wtmk+LdhK8n0jUCqKMvDzJ4Q9wPuBRpHPNY2MJHgju9iXp/VMhqK48gAW8Mk0q5KuXW6o9pwF+rIBZOcWVOmuVHiA0uqEw8zwJMQJdUjDXg17nZkfM9yGfd9XlCJF6nxCi2SZuRAAiYBNkSCZ+ICnyyXA0+tEQiNZWue/ZyzPI1GVBwgg6e3yeE4dGR6eygsJUf2jL+pn0zBfLQlDA6czQML9SPHS4KRq1fhV3dWnQXOmtUf1o0A...1mYHGwTmYG9wcyey8ATlqDJj4NmF/u6gWlQJUukZr/mceTsCwPbrD0+MXcDt2tYt8TfDfbXqKU2jTtMBDBuwJU12ydvNylECZqmjELjpOTwRu8tMcZP/09ueAU1eE4jQ22Jc8jBmP7ho+QmzUNQiwjPGIsLeyB4rAHTkt+ZIJJVVLUExJazd4/fWLXcKDARHCZaALJsKGeBpY5UKpip0871ikDAFWy27ufQtPOf632HZLBIpLeVzyJimjRIM4XMEnVyBu9VhUAwjxIFizAwblykYkqpp4w10tfHi8XcPXskqSGmX9sQH5xQP70IQV9GI49jtLoGWMjZOAOeOt0Leti/++nMpPa/4+jcz8BPw4Pa6no/fzAAAAABJRU5ErkJggg=="); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%;" type="text">
</div>
</div>
</form>
除了 $('#domainName').data('formValidation').resetForm();
还尝试过像这样更新表单元素:
$('#domainName').removeClass('fv-form-bootstrap');
$('#domainName').removeClass('fv-form');
$('#domainName').removeAttr('novalidate');
$('#domainName').data('formValidation').resetForm();
我错过了什么?
答案当然很简单:
$('#domainName').data('formValidation').destroy();
需要在不重新加载页面的情况下重复使用相同表单之前调用。
$('#domainName').data('formValidation').resetForm();
是一个转移注意力的问题,因为所有要做的就是清除字段,就像 HTML reset
表单按钮一样。
我在不同的动态生成 (jQuery) "dialogues" 中包括(多个实例)用于输入域名或 IP 地址的表单。第一个触发验证器,但后续的似乎没有。验证器设置大致如下所示:
function domainNamesPage(e){
getHTML('spf-tool', 'spf4');
nextButton.innerHTML = '<button class="btn">Next</button>';
$( ".form_blocks:last-child" ).after(addButton);
$( "#domainName" ).after(nextButton);
$('#domainName').formValidation({
framework: 'bootstrap',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
server: {
validators: {
callback: {
message: 'The address is not valid',
callback: function(value, validator, $field) {
if ( pass !== me ) {
// error_object
}
return true;
}
}
}
}
}
});
}
在第一个对话中,字段验证器按预期工作,但在随后的 "dialogues" 中似乎没有任何响应。
检查 form
元素本身:
$("form").each(function () {
console.log(this);
});
第一个看起来像这样:
<form id="domainName" class="dmarc_form" autocomplete="on" action="" method="">
后续像这样:
<form id="domainName" class="dmarc_form fv-form fv-form-bootstrap" autocomplete="on" action="" method="" novalidate="novalidate">
按此顺序加载的依赖项:
https://ajax.googleapis.com/ajax/libs/jquery/1.12.1/jquery.min.js
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css
https://formvalidation.io/vendor/formvalidation/css/formValidation.min.css
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
https://formvalidation.io/vendor/formvalidation/js/formValidation.min.js
https://formvalidation.io/vendor/formvalidation/js/framework/bootstrap.min.js
https://use.fontawesome.com/b66d6d44ad.js
https://cdn.fontawesome.com:443/js/stats.js
https://use.fontawesome.com/b66d6d44ad.css" media="all
而 HTML 基本上是:
<form id="domainName" autocomplete="on" action="" method="">
<div id="formGroup" class="form-group has-feedback">
<div class="input_validate_box">
<input class="form-control input_text bold" name="server" placeholder="Domain Name or IP 1" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAwZJREFUWAntVktoE2EQntlNugkltYoKgtDYrMdasQpFPOjBS7WmisWDoOBFEBE8Ciql2IsXEREEwYPWi4U+RMV6aY+tGkWK0NS0RjQ+KsVIbdrS7P5+G7qb3WRTY00EsT8k/7x2vsm8skQrZyUD/3sG2EjAI1Wtmk+LdhK8n0jUCqKMvDzJ4Q9wPuBRpHPNY2MJHgju9iXp/VMhqK48gAW8Mk0q5KuXW6o9pwF+rIBZOcWVOmuVHiA0uqEw8zwJMQJdUjDXg17nZkfM9yGfd9XlCJF6nxCi2SZuRAAiYBNkSCZ+ICnyyXA0+tEQiNZWue/ZyzPI1GVBwgg6e3yeE4dGR6eygsJUf2jL+pn0zBfLQlDA6czQML9SPHS4KRq1fhV3dWnQXOmtUf1o0A...1mYHGwTmYG9wcyey8ATlqDJj4NmF/u6gWlQJUukZr/mceTsCwPbrD0+MXcDt2tYt8TfDfbXqKU2jTtMBDBuwJU12ydvNylECZqmjELjpOTwRu8tMcZP/09ueAU1eE4jQ22Jc8jBmP7ho+QmzUNQiwjPGIsLeyB4rAHTkt+ZIJJVVLUExJazd4/fWLXcKDARHCZaALJsKGeBpY5UKpip0871ikDAFWy27ufQtPOf632HZLBIpLeVzyJimjRIM4XMEnVyBu9VhUAwjxIFizAwblykYkqpp4w10tfHi8XcPXskqSGmX9sQH5xQP70IQV9GI49jtLoGWMjZOAOeOt0Leti/++nMpPa/4+jcz8BPw4Pa6no/fzAAAAABJRU5ErkJggg=="); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%;" type="text">
</div>
</div>
</form>
除了 $('#domainName').data('formValidation').resetForm();
还尝试过像这样更新表单元素:
$('#domainName').removeClass('fv-form-bootstrap');
$('#domainName').removeClass('fv-form');
$('#domainName').removeAttr('novalidate');
$('#domainName').data('formValidation').resetForm();
我错过了什么?
答案当然很简单:
$('#domainName').data('formValidation').destroy();
需要在不重新加载页面的情况下重复使用相同表单之前调用。
$('#domainName').data('formValidation').resetForm();
是一个转移注意力的问题,因为所有要做的就是清除字段,就像 HTML reset
表单按钮一样。