Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Selectboxproblem mit Ajax und JQuery bzw. Javascript

Empfohlene Antworten

Hallo zusammen,

ich habe folgendes Problem:

Ich habe eine Selectbox. Diese hat eine eindeutige ID namens "id_supplier". Über JQuery und Ajax fülle ich die Selectbox mit meinen Options, die ich aus einer Datenbank hole. Möchte ich nun per Javascript oder JQuery den Value nach einem onchange-Event auslesen bekomme ich einen Fehler, dass die Variable nicht existiert.

Hier der Code mit dem ich die Options in die Selectbox hole ->


function supplierlist() {

    $.ajax({

        url : '/ajax/supplierlist/',

        success : function(data) {

            $('#id_supplier').html(data);

        }

    });

}

Das ist meine Liste, die ich mit Ajax hole ->

{% if customerlist %}

    <option value="0" selected="selected">---------</option>

{% for customer in customerlist %}

    <option value="{{ customer.id }}">{{ customer.name }}</option>

{% endfor %}

{% endif %}

Hier die Selectbox ->

<select name="supplier" id="id_supplier" onchange="selectsup();"></select>

Das ist meine Funktion, mit der ich mir den Wert aus der Selectbox holen will.

function selectsup() {

    console.log($('#id_supplier').val());

}

Mein HTML wird gefüllt und über das Safarientwicklertool habe ich die Liste auch gefüllt.

Jedoch bekomm ich in der Konsole folgenden Fehler:

ReferenceError: Can't find variable: supplierlist

Hat einer eine Idee? Vielen Dank im voraus!

Gruß, Chris.

[EDIT]Hoppla. Ich nehme alles zurück und behaupte das Gegenteil.[/EDIT]


function selectsup() {

    console.log($('#id_supplier').val());

}

da liegt dein fehler..

du musst auf das value vom selected index zugreifen und nicht auf das value vom select

Ted

Wie ungeschickt von mir. Ist natürlich logisch.

Vielen Dank für die Hilfe! :)

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Konto

Navigation

Suchen

Suchen

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.