Error with parsing xml file and appending it to dynamic dropdown menu
I have jquery functions to parse my xml file and append the result to particular div tag, but my OnMenuClick event is not firing and i am not able to display the result, please can i know where exactly i am going wrong?
function testmenu() {
$.ajax({
type: "POST",
url: "JsonWebService.asmx/TestXML",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "xml",
success:
function CreateMenus(results) {
for (var i = 0; i < results.length; i++) {
$("<div class='Menu'>" + results[i].Text + "</div>")
.click({ MenuId: results[i].MenuId }, OnMenuClick)
.appendTo("#accordionContainer");
}
},
error: function (msg) {
alert(xhr.statusText);
}
});
};
This is my function for OnMenuClick
function OnMenuClick(event) {
$("div[id ^= 'menuItemGroup']").slideUp(500);
$.ajax({
type: "POST",
url: "MenuItems.asmx/GetMenuItems",
data: '{"menuId":"' + event.data.MenuId + '"}',
contentType: "application/json; charset=utf-8",
dataType: "xml",
success: function (items) {
$(event.target).children().remove();
var html = "<div id='menuItemGroup" + event.data.MenuId + "' style='display:none'>";
for (var j = 0; j < items.length; j++) {
html += "<div id='MenuItem'> <a href='" + items[j].NavigateUrl + "'>" +
items[j].Text + "</a></div>";
}
html += "</div>";
$(e开发者_运维问答vent.target).append(html);
$("#menuItemGroup" + event.data.MenuId).slideDown(500);
},
error: function (err) {
alert(err.status + " - " + err.statusText);
}
});
};
My sample resultant xml file looks like this when i call the url MenuItems.asmx/GetMenuItems
ArrayOfMenuItem><MenuItem><MenuId>1</MenuId><MenuItemId>1</MenuItemId><Text>Books</Text><NavigateUrl>google.com</NavigateUrl></MenuItem><MenuItem><MenuId>1</MenuId><MenuItemId>2</MenuItemId><Text>Cd</Text><NavigateUrl>yahoo.com</NavigateUrl></MenuItem></ArrayOfMenuItem>
精彩评论