.validator('更新')不适用于动态添加的字段

时间:2017-12-14 08:20:15

标签: jquery twitter-bootstrap validation bootstrapvalidator-1000hz

我正在使用 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');

即使这不起作用。

有人能说出我的代码有什么问题,或者我错过了哪些内容?

1 个答案:

答案 0 :(得分:2)

这是一个有效的片段:

$("#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/