我正在尝试克隆bootstrap select字段,但在克隆后,cloned select不选择任何内容。
这是我的标记:
<div class="form-group car-list">
<label class="col-md-3 control-label">Select car #1</label>
<div class="col-md-9">
<div class="input-group">
<select class="select-car" data-live-search="true">
<option></option>
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
</div>
</div>
</div>
我的js:
$(document).ready(function() {
$('.select-car').selectpicker();
});
$('#add').click(function () {
var n = $('.car-list').length + 1;
var temp = $('.form-group:last').clone(true);
$('.control-label:first', temp).html('Select car #' + n);
$('.form-group:last').after(temp);
});
我尝试使用selectpicker refresh命令:结果是我在克隆div中有第二个selectpicker。
答案 0 :(得分:2)
<强> Working Fiddle 强>
您可以使用每个克隆中调用的select模型,然后在克隆之后初始化选择,如下面显示的代码段所示:
$('#add').click(function() {
var n = $('.car-list').length + 1;
var temp = $('.form-group:last').clone(true).removeClass('select-car');
$('.control-label:first', temp).html('Select car #' + n);
$('.input-group', temp).html($('#select-model').html());
$('.select-car', temp).selectpicker();
$('.form-group:last').after(temp);
});
希望这有帮助。
$('#add').click(function() {
var n = $('.car-list').length + 1;
var temp = $('.form-group:last').clone(true).removeClass('select-car');
$('.control-label:first', temp).html('Select car #' + n);
$('.input-group', temp).html($('#select-model').html());
$('.select-car', temp).selectpicker();
$('.form-group:last').after(temp);
});
.category {
margin-left: -12px;
font-size: 1.3em;
/*safari won't respect many/any of these but color?*/
/*font-style: italic;*/
font-weight: bold !important;
color: #000000 !important;
/*straight black makes it pop*/
/*background: #000;*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.5/css/bootstrap-select.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.5/js/bootstrap-select.min.js"></script>
<div class="form-group car-list">
<label class="col-md-3 control-label">Select car #1</label>
<div class="col-md-9">
<div class="input-group">
<select class="select-car selectpicker" data-live-search="true">
<option></option>
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
</div>
</div>
</div>
<div id="select-model" class="hide">
<select class="select-car" data-live-search="true">
<option></option>
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
</div>
<button id="add">Add</button>