gpt4 book ai didi

jquery - .validator ('update' )不适用于动态添加的字段

转载 作者:行者123 更新时间:2023-12-01 01:21:39 26 4
gpt4 key购买 nike

我正在使用Bootstrap(插件):validator.js v0.11.9 bootstrap validator用于 Python 项目中的字段验证(Python 版本 3.6 和 Django 版本 1.11)。在按钮click上,我使用jquery向表单添加了几个字段 -

JQuery:

 $("#button1Id").click(function(){
$("#divInsideFormId").append('filed1-selectoption, field2-datepicker, field3-textbox');
});

根据文档validator fields

默认情况下,验证器将仅验证插件初始化时存在的字段。如果您的表单有一组动态字段,则需要调用 $(...).validator('update') 来通知插件该组字段要验证的字段已更改。

我尝试了以下方法 -

$("#button1Id").click(function(){
$("#divInsideFormId").append('filed1-selectoption, field2-datepicker, field3-textbox');
$("#formId").validator('update');
});

这不起作用。我也尝试过 - $("#divInsideFormId").append(...).validator('update');

即使这样也不起作用。

任何人都可以建议我的代码有什么问题或者我是否遗漏了什么?

最佳答案

这是一个工作片段:

$("#button1Id").click(function(){

var html = '<label for="inputName" class="control-label">New numeric field</label><input type="number" class="form-control" id="inputNum" placeholder="new num field" required>';
$("#divInsideFormId").append(html);
$("#formId").validator('update');
});
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js"></script>


<form id="formId" data-toggle="validator" role="form">
<div id="divInsideFormId" class="form-group">
</div>

<div class="form-group">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" id="inputName" placeholder="Cina Saffary" required>
</div>
<div class="form-group has-feedback">
<label for="inputTwitter" class="control-label">Twitter</label>
<div class="input-group">
<span class="input-group-addon">@</span>
<input type="text" pattern="^[_A-z0-9]{1,}$" maxlength="15" class="form-control" id="inputTwitter" placeholder="1000hz" required>
</div>
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
<div class="help-block with-errors">Hey look, this one has feedback icons!</div>
</div>
<div class="form-group">
<label for="inputEmail" class="control-label">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email" data-error="Bruh, that email address is invalid" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label">Password</label>
<div class="form-inline row">
<div class="form-group col-sm-6">
<input type="password" data-minlength="6" class="form-control" id="inputPassword" placeholder="Password" required>
<div class="help-block">Minimum of 6 characters</div>
</div>
<div class="form-group col-sm-6">
<input type="password" class="form-control" id="inputPasswordConfirm" data-match="#inputPassword" data-match-error="Whoops, these don't match" placeholder="Confirm" required>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-group">
<div class="radio">
<label>
<input type="radio" name="underwear" required>
Boxers
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="underwear" required>
Briefs
</label>
</div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" id="terms" data-error="Before you wreck yourself" required>
Check yourself
</label>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
<button id="button1Id" type="button" class="btn btn-default">Add field</button>
</div>
</form>

或等效的jsfiddle:https://jsfiddle.net/beaver71/m90m178s/

关于jquery - .validator ('update' )不适用于动态添加的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47808771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com