使用jquery在下拉列表中填充xml

时间:2012-05-29 05:40:18

标签: c# jquery xml drop-down-menu

我想用jquery填充xml。我得到了以下代码:

jquery的:

   $.ajax({
        type: "GET",
        url: "GHworkerType.xml",
        dataType: "xml",
        success: function (xml) {
            $(xml).find('type').each(function () {
                var id = $(this).attr('id');
                var name = $(this).find('name').text();
                var rate = $(this).find('rate').text();
                $('<div id="link_' + id + '"></div>').html('<a href="' + name + '">' + type + '</a>').appendTo('#WorkerTypeDropDownList');

            });
        }
    });

的xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worker>
    <type id=0>
        <name>Fitter, mechanical or electrician</name>
        <rate>75</rate>
    </type>
</worker>

HTML:

<select id="WorkerTypeDropDownList">
      <option>loading</option>
</select>

我现在如何填充这个xml?我希望用户选择其中一个项目并将值(速率)传递给变量到我的代码隐藏(C#)。

我还想将这个xml填充到另一个下拉列表中。但在这种情况下,我想改变率。我怎么能让这个工作?

格尔茨

托比

编辑:

这是正在运行的代码:

JS:

$.ajax({
                                type: "GET",
                                url: "GHworkerType.xml",
                                dataType: "xml",
                                success: function (xml) {
                                    var select = $('#comboWorkerType');
                                    $(xml).find('type').each(function () {
                                        type = $(this).find('type').text();
                                        select.append("" + type + "");
                                        id = $(this).attr('id');
                                        name = $(this).find('name').text();
                                        rate = $(this).find('rate').text();
//                                        alert(rate);
                                        $('<option>' + name + '</option>').html('<option' + type + '">' + name + '</option>').appendTo('#comboWorkerType');
                                    });
                                    //                    alert($('#comboWorkerType option:selected').text());

                                    select.children(":first").text("Select worker type").attr("selected", true);

                                }

的xml:

<type>
    <name>Fitter, mechanical or electrician</name>
    <rate>75</rate>
</type>

HTML:

<select id="comboWorkerType" >
     <option>Select worker type</option>
</select>

但我希望将所选项目的费率计入我的代码隐藏。

我怎么能让它运行?

格尔茨

TOBI

0 个答案:

没有答案