Wie kann ich die Optionen aus meinem HTML-Select-Feld mit Javascript dynamisch einstellen? Dies ist meine Seite einrichten:Dynamisch setzen Auswahl-Optionen mit JavaScript
<form name="test">
<table>
<tr>
<td class='dataleft'>Product: </td>
<td><select name='inptProduct'></select></td>
</tr>
</table>
</form>
ich alle Werte in einem Array haben. Hier können Sie die <option>
s einstellen.
for(var i = 0; i < productArray.length; i++) {
console.log("<option value='" + productArray[i] + "'>" + productArray[i] + "</option>");
}
PS: jQuery kann verwendet werden.
Lösung: Dies ist die letzte Lösung, die ich suchte. Die neuen Optionen werden nicht auf das Auswahlfeld angewendet. Es entfernt alle zuerst, und fügt dann die neuen:
var optionsAsString = "";
for(var i = 0; i < productArray.length; i++) {
optionsAsString += "<option value='" + productArray[i] + "'>" + productArray[i] + "</option>";
}
$("select[name='inptProduct']").find('option').remove().end().append($(optionsAsString));
http://stackoverflow.com/questions/170986/what-ist-best-way-to-add-options-to-a-select-from-an-array-with-jquery –
http://stackoverflow.com/questions/47824/how-do-you-remove-all-the-options-of-a-select-box-and-then-add-one-option-and-se – AbiusX