Ich bin eine Drop-Down-Option über jQuery mit dem Gebäude folgenden:Wie kann ich OPTGROUP zur Select-Anweisung hinzufügen?
var myOptions = {
var1 : 'Road',
var2 : 'Rail'
};
var mySelect = $('#q54');
$.each(myOptions, function(val, text) {
mySelect.append(
$('<option></option>').val(text).html(text)
);
});
die Drop-Down-Gebäude, kein Problem - ich bin dann optgroup
Optionen anhänge versucht - ich habe in dem folgenden Code versucht, das Hinzufügen zu wickeln Die optgroup
um die Option aber keine Freude.
var i = 1; // Addition
$.each(myOptions, function(val, text) {
if(i == 1){
$("#q54").append('<optgroup class="optgroups" label="Team">');
}
mySelect.append(
$('<option></option>').val(text).html(text)
);
if(i == 2){
$("#q54").append('</optgroup>');
}
});
Die HTML-Ausgabe des oben ist:
<optgroup class="optgroups" label="Team"></optgroup>
<option value="Road">Road</option>
Die Ausgabe sollte:
<optgroup class="optgroups" label="Team">
<option value="Road">Road</option>
</optgroup>
Jede Beratung begrüßt.
http://api.jquery.com/wrapall/ – Pointy
'$ ("# Q54"). Append ('')' das ist nicht richtig, 'append' tut funktioniert nicht wie das Verketten von HTML. Wenn Sie '$ (" # q54 "). append ('
@RejithRKrishnan - macht Sinn - dann bin ich noch ratloser, was zu tun ist! –