<table class="table table-bordered">
<thead>
<tr>
<th width ="16%">合同号</th>
</tr>
</thead>
<tbody id="dataList">
<tr id="1" style="">
<td class="x-data">
<span class="VALUE" data-field="contractId" style="display: none;" data-code=""></span><span style="display: none;" class="SHOW"></span>
<select data-type="select" class="form-control x-select2 contract " value="" data-old-value="">
<option></option>
#foreach($item in $!{result.contracts})
<option value="$!{item.contractId}" data-supplierId="$!{item.supplierId}" data-supplier="$!{item.supplier}">$!{item.contractCode}</option>
#end
</select>
</td>
          <td>
           <a class="btn btn-success btn-xs x-add" href="javascript:void(0)"><i class="fa fa-plus"></i></a>
          </td>     </tr>
   </tbody>
</table>

直接clone()无作用!!

 var firstTR = $("#dataList tr:first");

        $(firstTR).find("td:last").find("a.x-add").click(function(){
addTR();
}); function addTR(){
  var tr = $(firstTR).clone(true);

  $("#dataList tr").eq(-2).before(tr);
}
以下的方式可以!!!
function addTR(){
// call destroy to revert the changes made by Select2
$("#dataList").find("select").select2("destroy");
// clone the row and insert it in the DOM
var tr = $(firstTR).clone(true);
$("#dataList tr").eq(-2).before(tr); // enable Select2 on the select elements
$("#dataList").find("select").select2();
}

先destory全部的select2,然后clone,clone之后,再全部激活select2!!!!解决!!!

参考:http://stackoverflow.com/questions/17175534/clonned-select2-is-not-responding

参考例子:

Before you clone the row, you need to disable Select2 on the select element by calling its destroymethod:

destroy

Reverts changes to DOM done by Select2. Any selection done via Select2 will be preserved.

See http://ivaynberg.github.io/select2/index.html

After you clone the row and insert its clone in the DOM, you need to enable select2 on both select elements (the original one and the new cloned one).

Here's a JSFiddle that shows how it can be done: http://jsfiddle.net/ZzgTG/

Fiddle's code

HTML

<div id="contents">
<select id="sel" data-placeholder="-Select education level-">
<option></option>
<option value="a">High School</option>
<option value="b">Bachelor</option>
<option value="c">Master's</option>
<option value="c">Doctorate</option>
</select>
</div>
<br>
<button id="add">add a dropdown</button>

CSS

#contents div.select2-container {
margin: 10px;
display: block;
max-width: 60%;
}

jQuery

$(document).ready(function () {
$("#contents").children("select").select2();
$("#add").click(function () {
$("#contents")
.children("select")
// call destroy to revert the changes made by Select2
.select2("destroy")
.end()
.append(
// clone the row and insert it in the DOM
$("#contents")
.children("select")
.first()
.clone()
);
// enable Select2 on the select elements
$("#contents").children("select").select2();
});
});
  

selec2 clone不起作用。的更多相关文章

  1. java克隆对象clone()的使用方法和作用

    转自:997.html">http://www.okrs.cn/blog/news/?997.html 内容摘要 若需改动一个对象,同一时候不想改变调用者的对象.就要制作该对象的一个本 ...

  2. clone()方法、深复制和浅复制

    clone方法 Java中没有明确提供指针的概念和用法,而实质上没个new语句返回的都是一个指针的引用,只不过在大部分情况下开发人员不需要关心如何去操作这个指针而已. 在实际编程中,经常会遇到从某个已 ...

  3. Prototype 原型模式 复制 浅拷贝 clone MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. 谨慎重载clone方法

    本文涉及到的概念 1.浅拷贝和深拷贝 2..clone方法的作用和使用方式 3.拷贝构造器和拷贝工厂   1.浅拷贝和深拷贝 浅拷贝 一个类实现Cloneable接口,然后,该类的实例调用clone方 ...

  5. 实战 MySQL 8.0.17 Clone Plugin(转)

    背景 很神奇,5.7.17 和 8.0.17,连续两个17小版本都让人眼前一亮.前者加入了组复制(Group Replication)功能,后者加入了克隆插件(Clone Plugin)功能.今天我们 ...

  6. git clone、 remote、fetch、pull、push、remote

    git clone命令笔记 作用:远程克隆版本库 1. 克隆版本库 git clone <版本库的网址> git clone zoran@192.168.2.167:/data/gitda ...

  7. 转发 java数据结构之hashMap详解

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

  8. Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

  9. Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例

    概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...

随机推荐

  1. WPF控件--利用Winform库中的NotifyIcon实现托盘小程序

    WPF控件--NotifyIcon   运行界面如下所示:            图1                                             图2 代码很少,如下所示 ...

  2. Owin中间件搭建OAuth2.0认证授权服务体会

    继两篇转载的Owin搭建OAuth 2.0的文章,使用Owin中间件搭建OAuth2.0认证授权服务器和理解OAuth 2.0之后,我想把最近整理的资料做一下总结. 前两篇主要是介绍概念和一个基本的D ...

  3. 【CodeForces 614A】Link/Cut Tree

    题 题意 给你一个区间,求里面有多少个数是k的次方. 分析 暴力,但是要注意这题范围会爆long long,当k=1e8: l=1:r=1e18时 k²=1e16,判断了是≤r,然后输出,再乘k就是1 ...

  4. OpenCV中的全景拼接例程

    使用Stitcher类,通过createDefault()方法创建拼接对象,通过stitch()方法执行默认的自动拼接.自动拼接和07年Brown和Lowe发表的论文描述的步骤基本一致,只不过使用的特 ...

  5. HD1847 Good Luck in CET-4 Everybody!(巴什博弈)

    巴什博弈: 一堆物品n个,最多取m个,最少取1个,最后取走的人获胜 分析:只要保证取玩最后剩m+1个,则必定胜利,所以构造m+1,只要n是 m+1的倍数,则先手必败,每次先手取玩,后手可取使得剩下的仍 ...

  6. 一大早居然有骗子还是傻子,真是莫名其妙的,QQ1913522040,一看就是刚申请不久的

  7. UILable点击事件

    UILabel *lLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 19, 105, 1)]; lLabel.backgroundColor ...

  8. rsyslog 与 logrotate 服务

    rsyslog与logrotate服务 rsyslog 负责写入日志, logrotate负责备份和删除旧日志, 以及更新日志文件. 一.rsyslog rsyslog 是一个 syslogd 的多线 ...

  9. WPF 窗体拖转时不触发MouseLeftButtonUpEvent

    解决方案:手动添加Handler,因为e.Handled这个属性是用在路由事件中的,当某个控件得到一个RoutedEvent,就会检测Handled是否为true,为true则忽略该事件. //手动注 ...

  10. SQL中CONVERT转化函数的用法

    格式:CONVERT(data_type,expression[,style])说明:此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,c ...