Javascript 在 jquery 中复制/克隆带有选定选项的下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3861729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:24:19  来源:igfitidea点击:

copy/Clone dropdown list with selected option in jquery

javascriptjquerydrop-down-menu

提问by Brij

How can I clone dropdown list(combobox) with selected option?

如何使用选定的选项克隆下拉列表(组合框)?

jquery .clone method is not working in firefoxfor selected option.

jquery .clone 方法在firefox 中不适用于所选选项。

I have a div having different controls. I have to copy entire div to a variable something like this

我有一个具有不同控件的 div。我必须将整个 div 复制到一个像这样的变量

var $orginalDiv = $('#myDiv');
var $clonedDiv = $orginalDiv.clone();

$clonedDiv.find('select').each(function() {


....Something do here for assigning selected options from original div ..

            });

Let me know how can we get it done and it must be worked in FireFox.

让我知道我们如何完成它,它必须在 FireFox 中工作。

回答by redsquare

var $orginalDiv = $('#myDiv');
var $clonedDiv = $orginalDiv.clone();

//get original selects into a jq object
var $originalSelects = $orginalDiv.find('select');

$clonedDiv.find('select').each(function(index, item) {

     //set new select to value of old select
     $(item).val( $originalSelects.eq(index).val() );

});

Try it hereat jsfiddle

试试这里的的jsfiddle

回答by triplethreat77

Can you implement a button to clone the drop downs?

你能实现一个按钮来克隆下拉菜单吗?

回答by Rikin Adhyapak

Hi you have a customedropdown with some image mask then simple clone will not display a selected value, for that you have to first assign a selected value of one dropdown to second drop down, and then call a change event implicitly like following. $('select[id*=cstate]').val($("select[id*=state]").val()); $("#cstate").change();

嗨,您有一个带有一些图像蒙版的自定义下拉列表,那么简单克隆不会显示选定的值,为此您必须首先将一个下拉列表的选定值分配给第二个下拉列表,然后隐式调用更改事件,如下所示。$('select[id*=cstate]').val($("select[id*=state]").val()); $("#cstate").change();