0

accessories1つのボタン( )を押すとselection(mylist)に1つの配列()が入力され、もう1つのボタン()を押すともう1つの配列()が選択accessoryDataに入力されるスクリプトを作成しました。ただし、コードの現在の状態では、2番目のボタンを押しても選択範囲が再入力されません。ここで何が問題になっていますか?weaponsweaponData

また、これを行うためのより効率的な方法がある場合は、それが役立つ可能性があります。

完全なコード

function runList(form, test) {
    var html = "";
    var x;
    dataType(test);
    while (x < dataType.length) {
        html += "<option>" + dataType[x];
        x++;
    }
    document.getElementById("mylist").innerHTML = html;
}
4

1 に答える 1

1

<select>ドロップダウンメニューなどの要素を作成しようとしていると思いますが、適切なマークアップは次のとおりです。

<select id="myList">
   <option value="1">Option 1</option>
   <option value="2">Option 2</option>
   <option value="n">Option n</option>
</select>

プロパティで<option>タグを閉じるのを忘れました。innerHTML

ネイティブJSメソッドの使用:

//create the select element
var select = document.create('select');
//or use getElementById if it's already there

//Assuming your datatype is an Array of string
var opt;
for(var i=0; i<datatype.length; i++){
  //create the option element
  opt = document.create('option');
  opt.textContent = datatype[i]; //innerText works too
  //set the value attribute
  opt.setAttribute('value',i+1); //+1 or else it would start from 0
  //add the option to the select element
  select.appendChild(opt);
  //conversely use removeChild to remove
}

//at last add it to the DOM if you didn't fetch it from the DOM.
于 2012-06-13T10:37:24.147 に答える