2012-02-28 20 views
0

コンボボックスにアイテムを特定の回数だけ追加する必要があります。これは私のコードです。コンボボックスに項目を追加する

  for(i=0;i<3;i++) 
    { 
     othercompaniesli.innerHTML= '<select onchange="document.location.href = this.options[this.selectedIndex].value;"><option VALUE="http://www.google.com">'+fStr1[0]+'</option> </select>'; 
    } 

は、ここで私はそれが唯一のtime.Thatがループのための働いているだけの項目値がappending.Only最後の値がコンボボックスに追加されていないです追加times.But fStr1列3を追加します。コンボボックスにアイテムを追加する方法を教えてください。

答えて

1
var tmpStr = '<select onchange="document.location.href = this.options[this.selectedIndex].value;">'; 

for(i=0;i<3;i++) 
{ 
    tmpStr+= '<option VALUE="http://www.google.com">'+fStr1[0]+'</option> '; 
} 

tmpStr = '</select>'; 

othercompaniesli.innerHTML = tmpStr; 
0

try othercompaniesli.innerHTML +=

0

あなたは=に等しい使用しているので、それは同じ要素に再割り当てされ

使用append()

$('#othercompaniesli').append('<select onchange="document.location.href = this.options[this.selectedIndex].value;"><option VALUE="http://www.google.com">'+fStr1[0]+'</option> </select>'); 

あなたselectoption要素が繰り返され注、あなたはそれに応じて変更する必要があります。

0
var select= $('mySelect'); 
var opt = new Option("OptionTitle", "123"); 

select.selectedIndex = InsertNewOption(opt, select[0]); 

function InsertNewOption(opt, element) 
{ 
    var len = element.options.length; 
    element.options[optsLen] = opt; 

    return len; 
} 
+0

、選択がそう選択として渡される必要が[0]又は選択するであろう代わり要素のjQueryオブジェクトであります.get(0) – steveukx

+0

ああ、確かに。どうも!修正しました。 – papaiatis

0

何をやっているループのうち場所selectタグ

 

var selectTag = '<select onchange="document.location.href = this.options[this.selectedIndex].value;">'; 
for(i=0;i<3;i++) { 
selectTag += '<option VALUE="http://www.google.com">'+fStr1[0]+'</option>'; 
} 
selectTag +="</select>" 
othercompaniesli.innerHTML = selectTag; 
 
0

ので、すべての要素が、それは選択開閉を所有する必要があります、あなたはあなたのselectタグを終了しているループの内側にありますタグ。あなたは最新の要素でinnerHTMLを更新しているだけです。その理由は、最後の要素を取得する理由です。ここで、 `` SELECT`とopt`もInsertNewOption` `に間違った順序で渡される

var openingTag= '<select onchange="document.location.href = this.options[this.selectedIndex].value;">'; 

for(i=0;i<3;i++) 
{ 
    openingTag+= '<option VALUE="http://www.google.com">'+fStr1[0]+'</option> '; 
} 

openingTag= '</select>'; 

othercompaniesli.innerHTML = openingTag; 
関連する問題