2017-02-10 7 views
2

私は、2つの異なる選択ボックスに入力するために使用したい配列を持っていますが、私はいくつかの奇妙な動作を見ています。最初の選択を入力すると問題はありませんが、同じリストを2番目の選択ボックスに追加すると、最初の選択がクリアされ、最後の項目が2番目の選択ボックスで選択されます。あなたはこのようなjQuery.cloneを使用してそれらのクローンを作成する必要が2つの選択ボックスにオプションの配列を入力しますか?

var optionList = [] 

optionList.push(new Option("waba", "waba")) 
optionList.push(new Option("shaba", "shaba")) 

$('#first_select').html(optionList); //works fine 
$('#second_select').html(optionList); //clears first select and last item is selected 

JS Fiddle Example

答えて

2

私はあなたが両方のためのHTMLコンテンツを設定optionList

var optionList = [] 

optionList.push(new Option("waba", "waba")) 
optionList.push(new Option("shaba", "shaba")) 

$('#first').html(optionList); 
$('#second').html($(optionList).clone()); 
1

$('#first').html(optionList); // append the original options 
$('#second').html($(optionList).clone()); // clone them and append the cloned ones 
3

のクローンを作成する必要があると思います1つのステートメントで:

$('#first, #second').html(optionList); 

jsfiddle:https://jsfiddle.net/tcuow32f/1/

+0

私はこの答えを好きですが、私の状況では簡単に動作しません。 –

関連する問題