Select2克隆仅工作两次 [英] Select2 clone is working only twice

查看:78
本文介绍了Select2克隆仅工作两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试克隆一个select2列表和2个文本区域,但是它仅适用于第一个克隆,而且我不明白为什么。任何新的眼光肯定都会有所帮助!
(克隆可以,但是select2不会应用到第三个克隆)

I'm trying to clone a select2 list and 2 text areas, but it's working only for the first clone, and I don't understand why..any new eye will certainly help ! (the cloning is OK, but the select2 is not applied to the 3rd clone)

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部分(我叫jquery并选择2个,当然)

JS part (I call jquery and select 2 before, of course)

$('#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
        });
        };
});

任何关于我的错误的主意吗?

Any idea of my mistake ?

推荐答案

查看此小提琴: http://jsfiddle.net/omugbdm1/3/ 这是一些代码混搭,但应该可以解决问题。

See this fiddle: http://jsfiddle.net/omugbdm1/3/ it's a bit of a mashup of code but should do the trick.

<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
    });

});

这篇关于Select2克隆仅工作两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆