문제

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>
도움이 되었습니까?

해결책

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

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

Have you tried that?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top