selec2 clone不起作用。
<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不起作用。的更多相关文章
- java克隆对象clone()的使用方法和作用
转自:997.html">http://www.okrs.cn/blog/news/?997.html 内容摘要 若需改动一个对象,同一时候不想改变调用者的对象.就要制作该对象的一个本 ...
- clone()方法、深复制和浅复制
clone方法 Java中没有明确提供指针的概念和用法,而实质上没个new语句返回的都是一个指针的引用,只不过在大部分情况下开发人员不需要关心如何去操作这个指针而已. 在实际编程中,经常会遇到从某个已 ...
- Prototype 原型模式 复制 浅拷贝 clone MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 谨慎重载clone方法
本文涉及到的概念 1.浅拷贝和深拷贝 2..clone方法的作用和使用方式 3.拷贝构造器和拷贝工厂 1.浅拷贝和深拷贝 浅拷贝 一个类实现Cloneable接口,然后,该类的实例调用clone方 ...
- 实战 MySQL 8.0.17 Clone Plugin(转)
背景 很神奇,5.7.17 和 8.0.17,连续两个17小版本都让人眼前一亮.前者加入了组复制(Group Replication)功能,后者加入了克隆插件(Clone Plugin)功能.今天我们 ...
- git clone、 remote、fetch、pull、push、remote
git clone命令笔记 作用:远程克隆版本库 1. 克隆版本库 git clone <版本库的网址> git clone zoran@192.168.2.167:/data/gitda ...
- 转发 java数据结构之hashMap详解
概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...
- Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...
- Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...
随机推荐
- iOS开发中的错误整理,(百思项目,指示器位置)设置控件尺寸和点坐标,先设置尺寸,再设置点坐标
之前对控件的尺寸和点的坐标的设置从来都是想到什么写什么,从来没有关心过顺序.然后就有了这次的血的教训!!!!! 下面是错误的截图,先设置的中心点,然后设置的宽度.程序运行就这样了,点别的没有毛病!!! ...
- hdu5536 字典树xor
一想到xor还要求最大类似的题,字典树效率高. 此代码c++ TLE. #include<stdio.h> #include<string.h> ; struct node { ...
- BZOJ2121 字符串游戏
Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其 他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,BX就可以选择是否将其删 ...
- 初次使用erlang的concurrent
如果不是它骇人听闻的并行性能,几乎不会考虑去学习这么一门语言.因为它的并行,我看到的是一块用软件写出来的电路板,是的,它几乎就是把电脑变成了一个可以自由编写逻辑的芯片. 例程来自这里:http://w ...
- Ubuntu添加开机自动启动程序的方法
文章出处:http://hi.baidu.com/gcc_gun/blog/item/fe9bbc4b84e911fa83025cb8.html 1. 开机启动时自动运行程序 Linux加载后, 它将 ...
- 输入你的性别,身高及体重,判断你的身材是否标准。(用if......else语句)
Console.WriteLine("请输入你的性别,身高和体重:"); string s = Console.ReadLine(); double h = double.Pars ...
- 求任意长度数组的最大值(整数类型)。利用params参数实现任意长度的改变。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- HTTP负载测试——Tsung
参考资料:http://blog.jobbole.com/87509/ 如何生成每秒百万级别的 HTTP 请求? 在进行负责测试时要牢记一件重要的事:你能在 Linux 上建立多少个 socket 连 ...
- git入门知识了解
文章转自:http://www.cnblogs.com/cocowool/archive/2012/02/17/2356125.html 源代码管理系统(SCM)与版本控制 版本控制是一种记录若干 ...
- iOS: imageIO完成渐进加载图片
imageIO完成渐进加载图片 不得不说,人都是有惰性的,一个月又快结束了,这个月虽说有点儿忙,但是绝对不差写几篇博客的时间,有时间去n次桌球厅,有时间玩n把英雄联盟,所谓小撸怡情大撸伤身,这个月游戏 ...