【发布时间】:2013-06-13 18:26:07
【问题描述】:
我在克隆现有选定元素(不是选项,而是整个选择框)并为它们创建动态 ID 时遇到问题。
我可以克隆所选元素,但它生成的 id 与所选父元素的 id 相同,并且不允许在其中选择选项。
当我点击新生成的选择框时,被克隆的父选择会显示要选择的选项列表,而不是选择的子选择。选择的孩子已冻结,我无法选择其中的选项。
截图:
JS:
$("#addCostDetailsRowBtn").button().click(addCostRowFn);
var addCostRowFn = function () {
var rowLen = $(".costTemplateRow").length;
//alert(rowLen);
var $newRow = $("#costTemplateRow1").clone(true);
$newRow.find('select').each(function () {
//alert($(this));
//alert($(this).attr('id'));
var item = $(this).attr('id');
if('undefined'!=item) {
var newItem = item.replace(/^(.*)(\d+)$/, function(match, p1, p2) {
return p1+(parseInt(p2)+1);
});
$(this).attr('id', newItem);
$(this).removeClass("chzn-done");
}
});
$('#costsTable tr:last').before($newRow);
return false;
};
有人可以帮我解决问题吗?
谢谢, 贾亚克里希纳
【问题讨论】:
-
是否有理由需要克隆 Chosen 生成的元素?克隆
-
这应该是一条评论
-
是的,我试过了。但由于某种原因,我无法在上面应用类(chzn-select)。
codevar $newRow = $("#costTemplateRow").clone(true); var rowLen = $(".costTemplateRow").length; $newRow.find('select').each(function (){ var curId = $(this).attr('id'); $(this).attr('id',curId+rowLen); $(this ).attr('class','chzn-select'); }); $newRow.show(); $('#costsTable tr:last').before($newRow);code -
谢谢,我尝试克隆一个简单的选择下拉菜单,然后在上面应用“选择”样式。我没有在所选课程上调用更新列表事件,因此之前没有工作。再次感谢您的投入。
标签: jquery select drop-down-menu clone