`

jquery的clone方法bug的修复

阅读更多
       本人原创,发现一些网站无道德的抓取,请自觉删去内容,转载请注明出处: http://asialee.iteye.com/blog/1753447

      项目中多次使用了iframe,但是操作起来是比较麻烦,项目中的现实情况是最外面是一个form,里面嵌套一个iframe,下面是一个其他的数据,在form提交的时候将iframe的数据和其他的数据一块提交。
     现在采用的是最原始的办法就是在form提交之前,将iframe里面的input,select,textarea都clone到外面的form里面,然后进行表单提交,随便说明一下,ifram表单里面可能有文件上传的input. 虽然感觉这个方法不好,但是比较简单,如果各位博友有好的办法欢迎一块讨论。直接上项目中的的代码吧:

$("iframe").find("input").each(function(){
	$("#formHidden").append($(this).clone());
});
$("iframe").find("select").each(function(){
	$("#formHidden").append($(this).clone());
});
$("iframe").find("textarea").each(function(){
	$("#formHidden").append($(this).clone());
});

      另外,测试发现,textarea和select的jquery的clone方法有问题,textarea和select的值clone的时候会丢掉,发现这个是jquery的一个bug,在网上发现一个插件,下载地址如下:https://github.com/spencertipping/jquery.fix.clone/blob/master/jquery.fix.clone.js,上不了的可以看下代码,比较简单。就是在clone的时候将val再重新赋值一下,如果知道这个了,也可以不用这个插件,自己写。
    

// Textarea and select clone() bug workaround | Spencer Tipping
// Licensed under the terms of the MIT source code license

// Motivation.
// jQuery's clone() method works in most cases, but it fails to copy the value of textareas and select elements. This patch replaces jQuery's clone() method with a wrapper that fills in the
// values after the fact.

// An interesting error case submitted by Piotr Przybył: If two <select> options had the same value, the clone() method would select the wrong one in the cloned box. The fix, suggested by Piotr
// and implemented here, is to use the selectedIndex property on the <select> box itself rather than relying on jQuery's value-based val().

(function (original) {
  jQuery.fn.clone = function () {
    var result           = original.apply(this, arguments),
        my_textareas     = this.find('textarea').add(this.filter('textarea')),
        result_textareas = result.find('textarea').add(result.filter('textarea')),
        my_selects       = this.find('select').add(this.filter('select')),
        result_selects   = result.find('select').add(result.filter('select'));

    for (var i = 0, l = my_textareas.length; i < l; ++i) $(result_textareas[i]).val($(my_textareas[i]).val());
    for (var i = 0, l = my_selects.length;   i < l; ++i) result_selects[i].selectedIndex = my_selects[i].selectedIndex;

    return result;
  };
}) (jQuery.fn.clone);

分享到:
评论

相关推荐

    jquery的clone方法应用于textarea和select的bug修复

    测试发现,textarea和select的jquery的clone方法有问题,textarea和select的值clone的时候会丢掉,发现这个是jquery的一个bug,上不了的可以看下代码,比较简单。就是在clone的时候将val再重新赋值一下,如果知道这个...

    jQuery EasyUI 1.4.1 离线简体中文API文档

    Bug(修复) combogrid:修复combogrid组件和其他combo组件高度不一致的问题; datagrid:修复在datagrid行元素调用“updateRow”方法的时候丢失某些类样式的问题; menubutton:修复在被禁用的按钮上使用...

    jquery-easyui-EDT-1.5.5.7z

    numberbox:'cloneFrom'方法可用; slider:'step'属性可以设置为浮点数; menu:'findItem'方法允许用户通过任意参数查找菜单项; menubutton:添加'showEvent'和'hideEvent'属性。 New Plugin(新组件) ...

    JQuery EasyUI 1.4.1 离线简体中文API文档

    Bug(修复) combogrid:修复combogrid组件和其他combo组件高度不一致的问题; datagrid:修复在datagrid行元素调用“updateRow”方法的时候丢失某些类样式的问题; menubutton:修复在被禁用的按钮上使用...

    jquery-easyui-1.4.1.zip

    更新日志 Bug The combogrid has different height than other...datetimebox: Add 'cloneFrom' method to create the datetimebox component quickly.(Datetimebox:加入“cloneFrom”方法来快速创建日期时间组件。)

    jQuery EasyUI 1.55API 中文版

    Bug(修复) tabs:修复当标题包含符合元素时,所选择的历史顺序错误的问题; combo:修复当设置了一个较大的'delay'值的时候下拉面板可能不会被隐藏的问题; layout:修复当鼠标光标快速离开的时候展开的面板...

    jQuery EasyUI 1.4.1 离线简体中文API文档 含完整开发工具包+皮肤+扩展+演示

    Bug(修复) combogrid:修复combogrid组件和其他combo组件高度不一致的问题; datagrid:修复在datagrid行元素调用“updateRow”方法的时候丢失某些类样式的问题; menubutton:修复在被禁用的按钮上使用...

    jQuery-Pulgins:我的日常工作 jQuery 插件

    Fixed the bug for the clone first li node . Optimize the code for find the ul node版本:1.0.2 日期 : 2013-10-21 11:10:06 笔记 : 。 添加滑块动画模式:默认为动画模式 . Support 3 kinds of mode : animat

    jQuery EasyUI 1.5.5 离线简体中文API文档 含完整开发工具包+皮肤+扩展+演示

    Bug(修复) • tabs:修复当标题包含符合元素时,所选择的历史顺序错误的问题; • combo:修复当设置了一个较大的'delay'值的时候下拉面板可能不会被隐藏的问题; • layout:修复当鼠标光标快速离开的时候展开...

    ChaZD-crx插件

    ChaZD 查字典,简洁易用的英汉字典扩展程序,支持划...项目代码开源在:https://github.com/ververcpp/ChaZD,程序还有待完善,欢迎Clone、使用、提交BUG,并提出您宝贵的意见与建议,非常感谢! 支持语言:中文 (简体)

    python入门到高级全栈工程师培训 第3期 附课件代码

    02 上节bug修复 03 编辑学生信息之前端功能 04 编辑学生信息之后台处理 05 以上内容总结 06 Ajax功能之dataType和traditional 第58章 01 今日内容概要 02 Ajax补充之serialize 03 分页功能介绍 04 分页基础知识 05...

    老男孩第三期Python全栈开发视频教程 零基础系统学习Python开发视频+资料

    ├─(130) 02 上节bug修复.avi ├─(131) 03 编辑学生信息之前端功能.avi ├─(132) 04 编辑学生信息之后台处理.avi ├─(133) 05 以上内容总结.avi ├─(134) 06 Ajax功能补充.avi ├─(135) day57课上所有.rar (22...

Global site tag (gtag.js) - Google Analytics