jquery selectpicker不适用于动态生成的内容

时间:2017-01-30 09:18:23

标签: javascript jquery html twitter-bootstrap

我使用以下库来设计选择框

https://silviomoreto.github.io/bootstrap-select/examples/

<select class="selectpicker" data-live-search="true">
  <option data-tokens="ketchup mustard">Hot Dog, Fries and a Soda</option>
  <option data-tokens="mustard">Burger, Shake and a Smile</option>
  <option data-tokens="frosting">Sugar, Spice and all things nice</option>
</select>

它适用于第一个div。如果我使用jquery动态生成项目,那么它不显示

$('#add_new_repair').click(function(){

 i++;

var mydata='<div class="item form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Model Parts Name <span class="required">*</span> </label> <div class="col-md-6 col-sm-6 col-xs-12"> <input id="brand_name" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" data-validate-words="2" name="name" placeholder="Model Name e.g Nokia 1100" required="required" type="text"> </div> <div class="col-md-3 col-sm-6 col-xs-12"> <button type="button" class="btn btn-primary" id="remove_repair_'+i+'" onclick="removeRow(this);> <span class="glyphicon glyphicon-minus" aria-hidden="true"> </span>Remove</button> </div> </div> <div class="item form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Model Parts Price <span class="required">*</span> </label> <div class="col-md-6 col-sm-6 col-xs-12"> <input id="brand_name" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" data-validate-words="2" name="name" placeholder="Price" required="required" type="text"> </div> </div> <div class="item form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Model Parts Warrenty<span class="required">*</span> </label> <div class="col-md-6 col-sm-6 col-xs-12"> <select id="basic" class="selectpicker show-tick form-control" data-live-search="true"> <option value="AK" selected>---Select--</option> <option value="ss">Alaska</option> <option value="HI">Nokia</option> <option value="CA">Motorolla</option> <option value="NV">Samsung</option> <option value="OR">Oregon</option> <option value="WA">Washington</option> <option value="AZ">Arizona</option> <option value="CO">Colorado</option> </select> </div> </div> <div class="item form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Model Image <span class="required">*</span> </label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="file" id="input03" class="form-control"> </div> </div> <div class="ln_solid"></div>';
   var content = '<select class="pincode"><option value="1">1</option><option value="2">2</option></select>';
 $("#dynamic_data").append(mydata);


});

3 个答案:

答案 0 :(得分:9)

您正在动态生成选择列表,因此在添加内容后,您必须通过以下行强制呈现动态生成的选择列表.<your_new_div_class_form> .<your_class_submit_button> { display: inline; }

selectpicker

答案 1 :(得分:3)

此外,对于selectpicker中html内的每次更改,您都需要调用refresh函数。我认为这比渲染更好。

$('.selectpicker').selectpicker('refresh');

答案 2 :(得分:1)

您必须使用on或动态创建的元素

$(document).on('click','#add_new_repair',function(){

// your code here

})