populating a drop down menu with xml file

后端 未结 2 1090
抹茶落季
抹茶落季 2021-02-10 01:32

I have the following code which is loading an xml file to populate a dropdown menu. Right now the value equals the option text but I would like to have the value equal to some n

2条回答
  •  耶瑟儿~
    2021-02-10 02:23

    First, start by putting the numbers in your xml file. If they are related to the dropdown item, i suggest as an attribute.

    example:

    
        Acura
        Aston Martin
        Audi
    ...
    
    

    then in your jquery loop:

    $(xml).find('dropdown').each(function(){
         $(this).find('make').each(function(){
              var value = $(this).attr('value');
              var label = $(this).text();
              select.append("");
         });
    });
    

    Note that you could probably simplify and speed up your jquery like this (untested):

    $('make', xml).each(function(){
                  var value = $(this).attr('value');
                  var label = $(this).text();
                  select.append("");
        });
    

    UPDATE

    For another, important, performance boost, do only one append() instead of as many append() as you have "make" nodes.

    var optionsHtml = new Array();
        $('make', xml).each(function(){
                      var value = $(this).attr('value');
                      var label = $(this).text();
    optionsHtml.push( "");
    
            });
    optionsHtml = optionsHtml.join('');
    select.append(optionsHtml);
    

提交回复
热议问题