【问题标题】:jquery ajax call returned datajquery ajax调用返回数据
【发布时间】:2014-08-07 10:02:26
【问题描述】:

我有以下 jquery ajax 调用,我需要知道如何在成功函数中找到“this”的名称/文本/id

function GetDropDownData() {
        // Get the DropDownList.
        var ddlTestDropDownListXML = $('#ddlTestDropDownListXML');

        // Provide Some Table name to pass to the WebMethod as a paramter.
        var tableName = "someTableName";

        $.ajax({
            type: "POST",
            url: "BindDropDownList.aspx/GetDropDownItems",
            data: '{tableName: "' + tableName + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                // Now find the Table from response and loop through each item (row).
                $(response.d).find(tableName).each(function () {
                    // Get the OptionValue and OptionText Column values.
                    var OptionValue = $(this).find('OptionValue').text();
                    var OptionText = $(this).find('OptionText').text();
                    // Create an Option for DropDownList.
                    var option = $("<option>" + OptionText + "</option>");
                    option.attr("value", OptionValue);

                    ddlTestDropDownListXML.append(option);
                });
            },
            failure: function (response) {
                alert(response.d);
            }
        });
    }

我有以下网络方法

 [System.Web.Services.WebMethod]
    public static string GetDropDownItems(string tableName)
    {
        // Create a dummy DataTable.
        DataTable dt = new DataTable(tableName);
        dt.Columns.Add("OptionValue");
        dt.Columns.Add("OptionText");
        dt.Rows.Add("0", "Item 0");
        dt.Rows.Add("1", "Item 1");
        dt.Rows.Add("2", "Item 2");
        dt.Rows.Add("3", "Item 3");
        dt.Rows.Add("4", "Item 4");

        // Convert the DataTable to XML.
        string result;
        using (StringWriter sw = new StringWriter())
        {
            dt.WriteXml(sw);
            result = sw.ToString();
        }

        return result;
    }

应该是$(this).ID 还是$(this).Text

我知道 response.d 返回 XML 如何访问通过 Jquery ajax 调用返回的 XML 文档中的标记名称 谢谢

【问题讨论】:

  • 我有同样的问题,谁能帮忙?

标签: jquery html asp.net ajax


【解决方案1】:

我不确定我是否完全理解了这个问题,但您可以从 $(this)(标准 jquery 元素)调用 prop('tagName')attr('ID')

xml = jQuery.parseXML('<Items><OptionValue>0</OptionValue><OptionText ID="12">Item 0</OptionText></Items>');

text = $(xml).find('OptionText');

$(text).prop('tagName') // Output: 'OptionText'
$(text).attr('ID')      // Output: '1'

作为后续,我建议在 ajax 请求中设置 dataType: 'xml',因为您已声明服务器将使用 XML 进行响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2011-12-01
    相关资源
    最近更新 更多