【发布时间】: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 文档中的标记名称 谢谢
【问题讨论】:
-
我有同样的问题,谁能帮忙?