2012-04-20 13 views
32

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)); 
+2

http://stackoverflow.com/questions/170986/what-ist-best-way-to-add-options-to-a-select-from-an-array-with-jquery –

+0

http://stackoverflow.com/questions/47824/how-do-you-remove-all-the-options-of-a-select-box-and-then-add-one-option-and-se – AbiusX

Antwort

32

wellyou haben es fast geschafft alles:

var optionsAsString = ""; 
for(var i = 0; i < productArray.length; i++) { 
    optionsAsString += "<option value='" + productArray[i] + "'>" + productArray[i] + "</option>"; 
} 
$('select[name="inptProduct"]').append(optionsAsString); 

EDIT entfernt $ Wrapper um letzte optionsAsString als append automatisch Strings konvertiert

+1

+1 würde wieder in meine Codebasis einfügen – vemv

+2

@vemv * wieder * - vielleicht ist es Zeit, eine Funktion zu erstellen, die dies tun – csharpfolk

19
var $inputProduct = $("select[name='inputProduct']") 

$(productArray).each(function(i, v){ 
    $inputProduct.append($("<option>", { value: v, html: v })); 
}); 
+0

das wird funktionieren - aber Das Hinzufügen der Optionen auf einen Schlag ist effizienter. Es spielt keine Rolle, ob es nur ein Pulldown auf einer Seite ist. – gotofritz

+0

Was lässt dich denken, dass das nicht so ist? – hunter

+1

Sie laufen .app für jedes Mitglied des ProduktsArray – gotofritz

-5

Sie können ID für die Auswahl id = inptProduct Dann tun $ ('#inptProduct'). Val (yourselectValue)

+0

Zuerst müssen Sie nicht festlegen die ID, um auf einen Knoten zuzugreifen, zweitens macht Val etwas völlig anderes. – gotofritz

+0

.val() wird den ausgewählten Wert des Auswahlelements festlegen. – Joe

1
function onLoad() { 

    var type = new Array("Saab","Volvo","BMW"); 

    var $select = $("select[name='XXXXXType']") 
    alert($select); 
    $(type).each(function(i, v){ 
     $select.append($("<option>", { value: v, html: v })); 
     }); 

} 
<select name="XXXXXType" id="XXXXXType"></select> 
+0

funktioniert nicht in der mobilen Anwendung –

2

Unter der Annahme, dass das Array productArray ist eine einfache Anordnung, wobei jedes Element der Wert der Option zu sein, und der gewünschte Titel für diese Option .

$('select[name="inptProduct"]') 
    .html('<option>' + productArray.join('</option><option>') + '</option>'); 

Beispiel:

productArray = ['One', 'Two', 'Three']; 

// With the original code being 

<select name="inptProduct"> 
    <option>There could be no options here</option> 
    <option>Otherwise, any options here will be overwritten</option> 
</select> 

// Running the code above would output 

<select name="inptProduct"> 
    <option>One</option> 
    <option>Two</option> 
    <option>Three</option> 
</select> 
0

Wenn Sie nicht für reine JavaScript beharren die Arbeit zu tun, können Sie den Job mit jQuery leicht getan.

<form name="test"> 
    <table> 
    <tr> 
     <td class='dataleft'>Product: </td> 
     <td><select id='inptProduct'></select></td> 
    </tr> 
    </table> 
</form> 


for (var i = 0; i < productArray.length; i++) { 
    $("#inptProduct").append('<option value=' + if_you_want_set_value + '>' + productArray[i] + '</option>'); 
    } 

Bitte beachte, dass ich hier die ID des select-Tag verwendet. Deshalb füge ich auch den HTML-Teil hinzu. Ich hoffe, Sie wissen, wie man jQuery hinzufügt.

0

Ich mache es in der Regel wie folgt aus:

document.getElementById("selectid").innerHTML="<option value='foo'>FOO</option>";

Verwandte Themen