Javascript 引导选择器在初始化时复制选择列表
在通过克隆html使用javascript动态创建选择列表之后,以及在选择列表创建之后,尝试使用Javascript 引导选择器在初始化时复制选择列表,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,在通过克隆html使用javascript动态创建选择列表之后,以及在选择列表创建之后,尝试使用$(MySelectList).selectPicker()初始化选择列表似乎成功了,但调用.selectPicker()后,它似乎重复了几个选择 如果没有.selectPicker()调用,克隆行中有三个下拉列表,但您无法更改值(列表会打开,但选择不会获取值) 调用.selectPicker()后,新行的下拉列表将获取选定的值,但现在总共有5个选择列表。两个附加列表是dup的 这就是它的全部内容:
$(MySelectList).selectPicker()初始化选择列表
似乎成功了,但调用.selectPicker()
后,它似乎重复了几个选择
如果没有.selectPicker()
调用,克隆行中有三个下拉列表,但您无法更改值(列表会打开,但选择不会获取值)
调用.selectPicker()
后,新行的下拉列表将获取选定的值,但现在总共有5个选择列表。两个附加列表是dup的
这就是它的全部内容:
<div id="advSearchRows" class="clearfix">
<!-- Form Row Template -->
<div class="row clone" id="ASR1">
<div class="form-group">
<select class="selectpicker include">
<option>Must Include</option>
<option>Must NOT Include</option>
</select>
</div>
<div class="form-group">
<select class="selectpicker match">
<option id="-1">foo</option>
<option id="0">bar</option>
<option id="1">fubar!</option>
</select>
</div>
<div class="form-group">
<input type="text" class="search-text form-control" placeholder="Keyword Text">
</div>
<div class="form-group remove hidden"></div>
</div>
</div>
必须包括
不得包括
福
酒吧
福巴!
和行重复代码
var numRows = $("#advSearchRows .clone").length; // how many rows currently
var lastRow = $("#advSearchRows .clone:last"); // the last row in the list
var lastId = lastRow.attr("id").replace("ASR", ""); // the id of the last item
if(numRows < 5) { // duplicate (clone) template row:
var id = parseInt(lastId) + 1; // the numeric id of the new row
var newRow = lastRow.clone().attr("id", id); // actual cloned row
newRow.find("select.include")
.attr("id", "include" + id)
.selectpicker(); // include ddl id
newRow.find("select.match")
.attr("id", "match" + id)
.selectpicker(); // match ddl id
newRow.insertAfter("div.clone:last").slideDown('slow');
$("#" + id).find(".remove").removeClass("hidden");
var numRows=$(“#advisarchrows.clone”).length;//当前有多少行
var lastRow=$(“#advisarchrows.clone:last”);//列表中的最后一行
var lastId=lastRow.attr(“id”).replace(“ASR”,即“);//最后一项的id
如果(numRows<5){//复制(克隆)模板行:
var id=parseInt(lastId)+1;//新行的数字id
var newRow=lastRow.clone().attr(“id”,id);//实际克隆的行
newRow.find(“select.include”)
.attr(“id”、“包括”+id)
.selectpicker();//包含ddl id
newRow.find(“select.match”)
.attr(“id”、“匹配”+id)
.selectpicker();//匹配ddl id
newRow.insertAfter(“div.clone:last”).slideDown(“slow”);
$(“#”+id.find(“.remove”).removeClass(“hidden”);
我想知道我的选择器是否有问题…
newRow.find(“select.match”).attr(“id”,“match”+id).selectpicker()
但根据文档,它看起来很合适……也许我遗漏了什么。很抱歉,我找到了解决方案。基本上我需要一个未初始化的隐藏模板,克隆模板,然后初始化。简单
//
// Handle "Add" row button event:
function cloneRow() {
var numRows = $("#advSearchRows .clone").length;
var lastRow = $("#advSearchRows .clone:last");
var template = $("#advSearchRows #ASR1");
if (numRows >= 6) return false;
var id = parseInt(lastRow.attr("id").replace("ASR", "")) + 1;
var newRow = template.clone().removeClass("hidden").attr("id", id);
newRow.find("select.include").attr("id", "include" + id).selectpicker();
newRow.find("select.match").attr("id", "match" + id).selectpicker();
newRow.insertAfter("div.clone:last").slideDown('slow');
$("#" + id).find(".remove").removeClass("hidden");
return false;
}
请不要使用“bootstrap”标签,使用“twitter bootstrap”,因为它意味着其他东西