2013-10-26 128 views
1

我想向包含“select2”(plugin)字段的表添加一行。虽然我得到新的行,但新的select2选择字段不起作用。任何想法为什么?无法克隆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的错误=未捕获的异常:为选择二没有定义的查询功能s2id_autogen3

+1

[Clonned Select2没有响应]的可能重复(http://stackoverflow.com/questions/17175534/clonned-select2-is-not-responding) –

回答

-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> 

这是真的 并没有被解雇选择2 s2id_autogen3没有定义迎战。

   <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> 

我是解决这个问题的方法。