无法克隆select2字段

时间:2013-10-26 10:57:50

标签: jquery select

我正在尝试向包含“select2”(plugin)字段的表添加一行。虽然我得到了新行,但新的select2选择字段不起作用。知道为什么吗?

p.s。:html是希腊语,这就是为什么我也不粘贴它,以避免混淆。我的猜测是它是一个jquery问题,但无论如何你需要html告诉我。

var rowtemplate = $("tr#1").html();
function createRow(num){
    var ns = "s" + num + "id_";
    var numAdd = num + 1;
    var autoGen = numAdd + 1;
    var psomi = rowtemplate.replace("_1", "_"+num);
    $("div.select2-container").remove(); // Remove all select2 elements
    return '<tr id="'+ num +'" class="troption">' + psomi + '</tr>';
}


$('a[data-role="addrow"]').click(function(event) {
    event.preventDefault();
    var lastTr =  $('tr.troption:last'),
        lastTrid = parseInt(lastTr.attr("id")),
        newid = lastTrid + 1;
        newRowHtml = createRow(newid);
        $('tr#'+lastTrid).after(newRowHtml);
        // Recreate the select2 divs
        $(".select2").select2({
                allowClear: true
            });
});

Firebug错误=未捕获异常:未为Select2定义查询函数s2id_autogen3

1 个答案:

答案 0 :(得分:-1)

如果您从页面源代码中复制代码并粘贴您自己的页面,则此错误会导致s2id_autogen3错误。

示例我从右键单击源代码中复制代码。

错误复制代码

<div class="form-group">
                    <label class="control-label col-md-3">Large</label>
                    <div class="col-md-4">
                        <div class="select2-container form-control input-large select2me" id="islemTipi">
                            <a href="javascript:void(0)" class="select2-choice select2-default" tabindex="-1">
                                <span class="select2-chosen" id="select2-chosen-4">Select...</span>
                                <abbr class="select2-search-choice-close"></abbr>
                                <span class="select2-arrow" role="presentation"><b role="presentation"></b></span>
                            </a><label for="s2id_autogen4" class="select2-offscreen"></label><input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-4" id="s2id_autogen4">
                        </div><select class="form-control input-large select2me select2-offscreen" data-placeholder="Select..." tabindex="-1" title="">
                            <option value=""></option>
                            <option value="AL">Alabama</option>
                            <option value="WY">Wyoming</option>
                        </select>
                        <span class="help-block"> </span>
                    </div>
                </div>

这是真的 并没有解雇select2 s2id_autogen3未定义vs ..

              <div class="form-group">
                    <label class="control-label col-md-3">Large</label>
                    <div class="col-md-4">
                        <select class="form-control input-large select2me" data-placeholder="Select...">
                            <option value=""></option>
                            <option value="AL">Alabama</option>
                            <option value="WY">Wyoming</option>
                        </select>
                        <span class="help-block">
                            .input-large
                        </span>
                    </div>
                </div>
我是这样解决的。