copy/Clone dropdown list with selected option in j

2019-02-06 03:24发布

问题:

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

jquery .clone method is not working in firefox for selected option.

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

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.

回答1:

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 here at jsfiddle



回答2:

Can you implement a button to clone the drop downs?



回答3:

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();