jquery的clone方法bug的修复
来源:互联网 发布:python运行 编辑:程序博客网 时间:2024/05/17 08:41
项目中多次使用了iframe,但是操作起来是比较麻烦,项目中的现实情况是最外面是一个form,里面嵌套一个iframe,下面是一个其他的数据,在form提交的时候将iframe的数据和其他的数据一块提交。
现在采用的是最原始的办法就是在form提交之前,将iframe里面的input,select,textarea都clone到外面的form里面,然后进行表单提交,随便说明一下,ifram表单里面可能有文件上传的input. 虽然感觉这个方法不好,但是比较简单,如果各位博友有好的办法欢迎一块讨论。直接上项目中的的代码吧:
Js代码 :
- $("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再重新赋值一下,如果知道这个了,也可以不用这个插件,自己写。
Js代码:
- // 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);
原创链接:http://www.software8.co/wzjs/jquery/2799.html
- jquery的clone方法bug的修复
- jquery的clone方法bug的修复
- jquery检测ie6的方法(修复了BUG)
- ubuntu iptables bug的修复方法
- 内存溢出bug的修复方法
- 重写 ccui.text 的clone方法。修复clone字体描边
- bug的修复
- 修复bug的思考
- jquery data 方法的bug
- 修复jQuery Autocomplete在FF中不支持中文的BUG
- jQuery EasyUI 1.3.1修复的bug和改善
- 修复jQuery UI Autocomplete在firefox不支持中文的BUG
- 修复jQuery Autocomplete在FF中不支持中文的BUG
- jquery-perfect-scoller.js的一个bug修复
- jquery的clone()陷阱
- jquery的clone用法
- jquery clone(true)和clone()的区别
- jquery clone 与 clone(true) 的区别
- 织梦(dedecms)仿站教程第三讲-路径问题
- PHP 也能达到JAVA的重载效果
- WebRTC的应用示例
- PHP学习笔记之——跳转
- windows2008 WEB服务器IIS7优化配置
- jquery的clone方法bug的修复
- 【linux环境配置】关于jre环境配置的一个错误
- C\C++文件读写操作
- 缓存依赖
- Redis复制与可扩展集群搭建
- 不定时定量更新文章对博客的影响
- [Unity3D]使用OnGUI函数显示状态栏并且使用触发器修改玩家当前信息
- CRC 校验
- 谈谈个人的对新博客的看法