2015-03-25 107 views
0

我想克隆一个select2列表和2个文本区域,但它只适用于第一个克隆,我不明白为什么......任何新的眼睛肯定会有所帮助! (克隆是OK,但是SELECT2没有被施加到所述第三克隆)Select2克隆工作只有两次

HTML部分

<fieldset> 
<div id="test"> 
<div> 
<label>Tool Name : </label> 
<select class="toollist" name="FSR_tool_id[]" style="width: 350px" /> 
<option></option> 
<option value="1" >bla 1</option> 

</select> 
<input type="Button" value="ADD ANOTHER TOOL" class="AddTool"> 
<label>Service Scope</label> 
<textarea rows="5" style="width:99%" name="FSR_servicescope[]" class="validate[required]" /> 
</textarea> 
</br> 
<label>Service Description</label> 
<textarea rows="10" style="width:99%" name="FSR_servicedesc[]" class="validate[required]" /> 
</textarea><hr> 
</div> 
</div> 
<input type="hidden" value="0" id="countertool"> 
</fieldset> 

JS部分(I调用jQuery和前选择2,当然)

$('#test .toollist').select2({ //apply select2 to my element 
     placeholder: "Search your Tool", 
     allowClear: true 
     }); 

$("input[type='button'].AddTool").live('click', 
function() { 
    var index = $(this).closest('div').index(); 
    if (index > 0) { 
     $(this).closest('div').remove(); 
    } else { 

     $('#test .toollist').select2('destroy'); 
     //we have to destroy the select2 before cloning 
     var $div = $(this).closest('div').clone(true); 
     $div.find('input.AddTool').val("DELETE THIS TOOL"); 
     //to replace the button "add" by another "delete" 
     var $input = $div.find('input.exp'); 
     var index = $('input#countertool').val(); 
     var id = 'exp' + index; 
     index++; 
     $('input#countertool').val(index); 
     $input.attr('id', id).data('index', index); 
     $(this).closest('#test').append($div); 
     //then, we re-apply select2 to the lists    
     $('#test .toollist').select2({ 
     placeholder: "Search your tool !", 
     allowClear: true 
     }); 
     }; 
}); 

任何想法我的错误?

非常感谢!

+0

你有没有得到这个排序? – 2015-08-12 04:12:30

+0

不!但是由于在我的系统中有3个克隆是非常罕见的,所以我暂时不花太多时间来解决它;-)编辑:对不起,我没有看到你的例子,我会尝试它,并会尽快告诉你! – 2015-09-15 10:18:21

回答

0

看到这个小提琴:http://jsfiddle.net/omugbdm1/3/这是一个代码混搭,但应该做的伎俩。

<div id="test"> 
    <div id="tooltest0" class="tooltest0"> 
     <label>Tool Name :</label> 
     <select class="toollist" name="FSR_tool_id[]" id="FSR_tool_id0" style="width: 350px" /> 
      <option></option> 
      <option value="1">bla 1</option> 
     </select> 
    </div> 
    <div id="tool-placeholder"></div> 
    <div> 
     <input type="button" value="Add another" /> 
    </div> 
</div> 

$('.toollist').select2({ //apply select2 to my element 
placeholder: "Search your Tool", 
allowClear: true 
}); 


$('input[type=button]').click(function() { 

    $('.toollist').select2("destroy"); 
    var noOfDivs = $('.tooltest0').length; 
    var clonedDiv = $('.tooltest0').first().clone(true); 
    clonedDiv.insertBefore("#tool-placeholder"); 
    clonedDiv.attr('id', 'tooltest' + noOfDivs); 


    $('.toollist').select2({ //apply select2 to my element 
    placeholder: "Search your Tool", 
    allowClear: true 
    }); 

}); 
+0

我试图适应你的解决方案,但我无法设法使“删除”按钮出现并工作......当我克隆HTML块(包含select,按钮ADD - 或删除克隆元素 - 和2个文本区域),我需要在DELETE中转换ADD按钮以销毁克隆,如果我想... – 2015-09-15 10:43:11

+0

如果您将代码放在某处,我可以看一看,如果您愿意...... – 2015-09-15 20:23:36