Jak mogę dynamicznie ustawić opcje z mojego pola wyboru html przy pomocy Javascript? To jest moje ustawienia strony:Dynamicznie ustaw opcje select z JavaScript
<form name="test">
<table>
<tr>
<td class='dataleft'>Product: </td>
<td><select name='inptProduct'></select></td>
</tr>
</table>
</form>
mam wszystkie wartości w tablicy. To jest miejsce, w którym można ustawić s. <option>
.
for(var i = 0; i < productArray.length; i++) {
console.log("<option value='" + productArray[i] + "'>" + productArray[i] + "</option>");
}
PS: jQuery może być używany.
Rozwiązanie: Jest to ostateczne rozwiązanie szukałem. Nie stosuje nowych opcji do pola wyboru. Usuwa wszystko, a potem dodaje nowe:
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-is-the-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