用“文本"在XML中填充下拉菜单. & “值" [英] Populate Dropdown from XML with"text" & "value"

查看:71
本文介绍了用“文本"在XML中填充下拉菜单. & “值"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个下拉菜单,该菜单从xml文件中提取.但是,我希望xml中每个选项的值"在下拉选项中也可见.因此,使用下面的xml代码,下拉文本将如下所示:

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

我也该如何提取值?

    //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'),

您尝试过吗?

这篇关于用“文本"在XML中填充下拉菜单. &amp; “值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆