我有以下jquery调用,我需要知道如何在success函数中找到"this“的名称/text/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);
}
});
}我有以下的And方法
[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调用生成的XML文档中的标记名,谢谢
发布于 2014-08-17 01:58:26
我不确定我是否完全理解这个问题,但是通过$(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进行响应。
https://stackoverflow.com/questions/25179600
复制相似问题