jQuery 克隆链接选择

2024-01-12

我刚刚开始:http://jsfiddle.net/FJFFJ/1/ http://jsfiddle.net/FJFFJ/1/ (by 使用 JQuery 链接动态创建的下拉菜单 https://stackoverflow.com/q/5545229/1513125)

这真的很好,但现在我需要做一些改变:克隆最后一组选择。

ie.:

+-
Argentina | San Juan | Rawson
Chile     | Santiago | Chiñihue

然后,如果我单击“+”,它将克隆

Chile | Santiago | Chiñihue

而不是第一个。


这实际上是一个比我想象的更难的问题。显然,当您克隆 SELECT 元素集时,它无法更改为未显示的内容。我花了大约一个小时左右才弄清楚到底发生了什么,很好的挑战,谢谢!

我最终要做的是克隆模板并手动更改值并手动调用“更改”事件,以便在辅助和三元 SELECT 元素中提供正确的选项。

版本1:http://jsfiddle.net/m4JTQ/2/ http://jsfiddle.net/m4JTQ/2/

版本 2(这是一个修改版本,删除了i迭代器:http://jsfiddle.net/Zf7xb/1/ http://jsfiddle.net/Zf7xb/1/

这是代码,以防 jsfiddle 最终消失。

// Version 1
$(function() {

    // Iterator for the dupe ids
    var i = 0;

    $('#clone').click(function() {
        // put the clone() into cloned
        var cloned = $('#template').clone();

        // Add .dupe class to cloned
        $(cloned).addClass('dupe');

        // Set the id of cloned, use i++ instead of incrementing it elsewhere.
        $(cloned).attr('id', 'duplicate'+ i++);

        // Append cloned to #filter
        $(cloned).appendTo('#filter');

        // Passing selector rather than iteration                   
        chainItWithId($(cloned));

        // If this is NOT the first addition, do some kludge
        if ($('#filter div.dupe').length!==1) {
            // Set the previous clone to lastClone
            var lastClone = $(cloned).siblings('div.dupe:last');

            // Set the value of .pais to the value of the previous .pais
            $(cloned).find('.pais').val($(lastClone).find('.pais').val());
            // Do the "change" event manually.
            $(cloned).find('.pais').change();

            // Set the value of .provincia to the value of the previous .provincia
            $(cloned).find('.provincia').val($(lastClone).find('.provincia').val());
            // Do the "change" event manually
            $(cloned).find('.provincia').change();

            // Set the value of .ciudad to the value of the previous .cudad
            $(cloned).find('.ciudad').val($(lastClone).find('.ciudad').val());

        }

        // Show the hidden div
        $('#filter div:hidden').show();

    });
    $('#remove').click(function() {
        // Remove all but the very last set of options
        if ($('#filter > div').length > 1) {
            $('#filter > div').last().remove();
        }
    });

    // Manually do the "click" event
    $('#clone').click();
});

// Here I'm getting the cloned full selector
function chainItWithId(cloned) {
    // Chain .provincia to .pais in the current clone
    $(cloned).find('.provincia').chained($(cloned).find('.pais'));
    // Chain .ciudad to .provincia in the current clone
    $(cloned).find('.ciudad').chained($(cloned).find('.provincia'));
}

No i这个版本中的迭代器,它更干净一些。

// Version 2
$(function() {

    $('#clone').click(function() {
        // put the clone() into cloned
        var cloned = $('#template').clone();

        // Add .dupe class to cloned
        $(cloned).addClass('dupe');

        // Set the id to the count of div.dupe elements in #filter
        // This will increment 0,1,2,3 as you add elements.
        $(cloned).attr('id', 'duplicate'+ $('#filter div.dupe').length);

        // Append cloned to #filter
        $(cloned).appendTo('#filter');

        // Passing selector rather than iteration                    
        chainItWithId($(cloned));

        // If this is NOT the first addition, do some kludge
        if ($('#filter div.dupe').length!==1) {
            // Set the previous clone to lastClone
            var lastClone = $(cloned).siblings('div.dupe:last');

            // Set the value of .pais to the value of the previous .pais
            $(cloned).find('.pais').val($(lastClone).find('.pais').val());
            // Do the "change" event manually.
            $(cloned).find('.pais').change();

            // Set the value of .provincia to the value of the previous .provincia
            $(cloned).find('.provincia').val($(lastClone).find('.provincia').val());
            // Do the "change" event manually
            $(cloned).find('.provincia').change();

            // Set the value of .ciudad to the value of the previous .cudad
            $(cloned).find('.ciudad').val($(lastClone).find('.ciudad').val());

        }

        // Show the hidden div
        $('#filter div:hidden').show();

    });
    $('#remove').click(function() {
        // Remove all but the very last set of options
        if ($('#filter > div').length > 1) {
            $('#filter > div').last().remove();
        }
    });

    // Manually do the "click" event
    $('#clone').click();
});

// Here I'm getting the cloned full selector
function chainItWithId(cloned) {
    // Chain .provincia to .pais in the current clone
    $(cloned).find('.provincia').chained($(cloned).find('.pais'));
    // Chain .ciudad to .provincia in the current clone
    $(cloned).find('.ciudad').chained($(cloned).find('.provincia'));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery 克隆链接选择 的相关文章

随机推荐

  • 从命令行使用网络摄像头拍照[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我有一个 Logitech 网络摄像头 适用于笔记本电脑的 QuickCam Pro 我想从命令行使用该摄像头拍摄快照 它有命令行界面吗 如果没有 是
  • 织物密码

    每次结构运行时 它都会要求输入 root 密码 是否可以将其与自动建议一起发送 fab staging test 我知道您已经询问过密码 但是配置系统以便您可以在没有密码的情况下进行结构 即 SSH 不是更好吗 为此 在本地计算机上执行以下
  • Julia 中向量的就地重排?

    是否可以在给定索引列表的情况下重新排列向量中的值 我有两个数组 我想排序arr2基于arr1两者都是预先分配的 indices zeros length arr1 sortperm indices arr1 arr2 indices lt
  • T4 输出到字符串

    我是 T4 的新手 我想生成 CRUD 存储过程 数据访问层 输出到字符串 我正在尝试创建一种对其他程序员也有用的 Web 服务 api 只需输入数据 然后下载 zip 文件 我已经搜索了很多 但找不到一种生成字符串输出的方法 是否可能 任
  • 在 C++ 中禁用复制省略

    免责声明 研究的目标是如何禁用所提供代码部分的复制省略和返回值优化 如果想提及诸如 XY 问题之类的问题 请避免回答 该问题具有严格的技术和研究性质 并且以这种方式明确提出 C 14 中引入了复制省略和返回值优化 如果某个对象已在一个表达式
  • 跨多个文件的 Javascript 中的全局变量

    我的一堆 JavaScript 代码位于名为 helpers js 的外部文件中 在调用此 JavaScript 代码的 HTML 中 我发现自己需要知道是否调用了 helpers js 中的某个函数 我试图通过定义创建一个全局变量 var
  • Log4J:警告无法为 Logger 错误找到 Appender

    下面是我的log4j properties文件内容 文件与eclipse中的src文件夹放在一起 Application Logs log4j rootlogger INFO logg log4j appender logg org apa
  • Facebook 应用程序是使用 PhoneGap 或 Appcelerator 等移动开发框架构建的吗?

    我发现了numerous http venturebeat com 2011 07 08 hybrid mobile apps take off as html5 vs native debate continues sources htt
  • node.js http.get 在对远程站点发出 5 次请求后挂起

    我正在编写一个简单的 api 端点来确定我的服务器是否能够访问互联网 它工作得很好 但是在 5 个请求之后 每次都是 5 个 请求就会挂起 当我将 Google 切换到 Hotmail com 时 也会发生同样的情况 这让我觉得这是我的事情
  • 从另一个应用程序启动时,应用程序将失去记住其堆栈的能力

    现在我已经对此进行了更多研究 我写这篇文章是为了让它更清楚 如果您正在寻找更多信息 可以在旧版本中找到一些信息 怎么了 这是指没有设置任何launchMode的应用程序 设置 因此使用默认值 您可以从市场或安装程序启动应用程序 这 启动应用
  • 将特定风格的依赖关系与维度链接起来

    我正在开发一个 Android 应用程序 它有两种类型 免费和付费 每层都有 2 个版本 轻型和重型 这是 Gradle 的实现 flavorDimensions tier distro productFlavors free dimens
  • Java 10:替换 java.xml.ws 冲突

    我必须使用java xml ws 我的项目中包含一些组件 但因为它已被弃用并且很快就会被删除 所以我想使用这些组件的替代品 所以我将此依赖项添加到我的项目中pom file
  • 如何从 Dart 的 List 中获取随机元素?

    如何从 Dart 集合中检索随机元素 var list a b c d e import dart math var list a b c d e generates a new Random object final random new
  • 为什么一个字节只有0到255?

    为什么一个字节的范围只有0到255 严格来说 术语 字节 实际上可以指具有 256 个值以外的单元 只不过是这样而已几乎普遍尺寸 从维基百科 http en wikipedia org wiki Byte 从历史上看 一个字节是 用于编码单
  • 错误 c4996 检查迭代器

    我使用 VC 2013 我有以下代码 pragma warning disable 4996 define D SCL SECURE NO WARNINGS include
  • LinqToSql 和 WCF

    在使用 WCF 服务与数据库交互的 n 层应用程序中 在整个应用程序中使用 LinqToSql 类的最佳实践方法是什么 我见过它以几种不同的方式完成 但它们似乎花费了大量时间来创建额外的接口 消息类等 这减少了您无需编写数据访问代码而获得的
  • 一次按多个键让我的角色沿对角线移动

    我遇到的问题是 当用户按下 K UP 键和 K RIGHT 键或 K UP 键和 K DOWN 键等时 我试图使我的角色在屏幕上对角移动 这是我的角色移动代码 事件处理 1 Event Handling 2 for event in pyg
  • 无法加载文件或程序集“ServiceStack”或其依赖项之一。该系统找不到指定的文件

    我试图将我的网络应用程序启动到服务器 Server Windows Server 2008 R2 企业版 IIS版本 7 5 框架版本 4 0 30319 17929 但出现以下错误 Server Error in salavirtual
  • 绑定转换器参数

    有没有办法我可以做到这一点Style
  • jQuery 克隆链接选择

    我刚刚开始 http jsfiddle net FJFFJ 1 http jsfiddle net FJFFJ 1 by 使用 JQuery 链接动态创建的下拉菜单 https stackoverflow com q 5545229 151