Wie bekomme ich alle “Option” -Werte in “Select” von Jquery?

Lesezeit: 2 Minuten

Benutzer-Avatar
Jin Yong

Weiß jemand, wie ich alle Werte in der Auswahl von Jquery erhalten kann?

Beispiel:

<select name="group_select" id="group_select">
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
</select>

Ich möchte alle Optionswerte (a, b und c) von select (ohne ausgewählte Option) mithilfe von jquery abrufen

  • mögliches Duplikat von How to get all options of a select using Jquery?

    – PhoneixS

    9. Juli 2013 um 10:24 Uhr

var values = $("#group_select>option").map(function() { return $(this).val(); });

  • Ich musste am Ende ein .get() hinzufügen … var values = $("#group_select>option").map(function() { return $(this).val(); }).get();

    – plong0

    12. Februar 2015 um 8:01 Uhr


  • @plong0 ja, wir müssen val() verwenden, um es in Array zu bekommen

    – PriyankMotivaras

    14. Februar 2020 um 11:41 Uhr

Im Folgenden erhalten Sie ein Array der <option> Werte

var values = $.map($('#group_select option'), function(e) { return e.value; });

// as a comma separated string
values.join(',');

Hier ist ein Arbeitsdemo. hinzufügen /bearbeiten auf die URL, um den Code zu sehen.

  • Danke für values.join(','); diese Antwort war eher auf meine Lösung anwendbar.

    – HPWD

    8. Oktober 2012 um 14:58 Uhr

  • Einzelne Zeile: var $('#group_select option').map(function() { return this.value; }).get().join(); [link(api.jquery.com/map)

    – JoePC

    Jul 26, 2018 at 21:39

user avatar
helloandre

This will iterate through all the options and give you their values.Then you can either append them to an array or manipulate them individually.

$('#group_select option').each(function(){
    $(this).val()
};

  • I think closing bracket of each loop missing.

    – Vinay Kaithwas

    Mar 13, 2020 at 6:08


You can use following code for that :-

var assignedRoleId = new Array();
$('#RolesListAssigned option').each(function(){
        assignedRoleId.push(this.value);
        assignedRoleId.push(this.text);
});

1144640cookie-checkWie bekomme ich alle “Option” -Werte in “Select” von Jquery?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy