Frage

I have created a dropdown menu that pulls from an xml file. However, I would like the "value" of each option in the xml to also be visible in the dropdown option. So with the below xml code the dropdown text would look like this:

 Doctor 1
 Dentist 2
 Vet 3

etc.

How do I pull the value as well?

    //XML
    <ps>
    <specialty value="1">Doctor</specialty>
    <specialty value="2">Dentist</specialty>
    <specialty value="3">Vet</specialty>
    <ps>

    $(document).ready(function () {
    var ps_data;

    // Loading xml
      $.get('test.xml', function (data) {
        ps_data = data;
        var ps = $('#special');
        $('specialty', ps_data).each(function () {
            $('<option />', {
                text: $(this).text(),
                value: $(this).attr('index')
            }).appendTo(ps);
          });     
        }, 'xml');
       });


    //HTML
    <div>
       <select name="Count_1" class="special" id="special">
          <option value="">Select Specialty</option>
       </select>

    </div>
War es hilfreich?

Lösung

All you should have to do is update your text line to the following:

text: $(this).text()+ " " + $(this).attr('value'),

Have you tried that?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top