I'm trying to create a menu and its sub menu, but receiving the data from database. And I created the menu successfully, this is my code :
我正在尝试创建一个菜单及其子菜单,但是从数据库接收数据。我成功创建了菜单,这是我的代码:
$(document).ready(function () {
var url = '<%: Url.Content("~/") %>' + "Products/GetMenuList";
$.getJSON(url, function (data) {
var mainMenu = $("#content ul#navmenu-v");
$.each(data, function (index, dataOption) {
var new_li = $("<li id='level1'><a href='javascript:void(0);' id='"
+ dataOption.ID + "' class ='selectedcategory'>" +
dataOption.Name + "</a>");
mainMenu.append(new_li);
});
$('.selectedcategory').mouseover(function () {
$("ul#subCat").empty();
$("li#level1").append("<ul id='subCat'>");
var urlCat = '<%: Url.Content("~/") %>' + "Products/GetCategoryList";
$.getJSON(urlCat, { Depid: this.id }, function (dataCat) {
$.each(dataCat, function (indexCat, dataOptionCat) {
$("ul#subCat").append("<li><a href='javascript:void(0);'
class='selectedsubcat' id='" + dataOptionCat.id + "'>" +
dataOptionCat.Name + "</a></li></ul></li>");
});
});
});
});
});
This is the HTML :
这是HTML:
<div id="content">
<ul id='navmenu-v'>
</ul>
</div>
But it didn't get the right sub menu due to the main menu. Could anyone show me some suggestion please.
但由于主菜单,它没有得到正确的子菜单。请问有人给我一些建议。
Thanks so much.
非常感谢。
2 个解决方案
#1
1
Try this looks like what you want I hv simulated it here http://jsfiddle.net/d4udts/W9cCE/5/
尝试这看起来像你想要的我在这里模拟它http://jsfiddle.net/d4udts/W9cCE/5/
$(document).ready(function () {
var url = '<%: Url.Content("~/") %>' + "Products/GetMenuList";
$.getJSON(url, function (data) {
var mainMenu = $("#content ul#navmenu-v");
$.each(data, function (index, dataOption) {
var new_li = $("<li class='level1'><a href='javascript:void(0);' id='"+ dataOption.ID + "' class ='selectedcategory'>" + dataOption.Name + "</a>");
mainMenu.append(new_li);
});
$('.level1').hover(function(){
$(this).append("<ul class='subCat'></ul>");
var ul=$(this).children('ul');
var urlCat = '<%: Url.Content("~/") %>' + "Products/GetCategoryList";
$.getJSON(urlCat, { Depid: $(this).find('a.selectedcategory').attr('id')}, function (dataCat) {
$.each(dataCat, function (indexCat, dataOptionCat) {
$(ul).append("<li><a href='javascript:void(0);'" +
"class='selectedsubcat' id='" + dataOptionCat.id + "'>" +
dataOptionCat.Name + "</a></li>");
});
});
},function(){
$(this).children('ul').remove();
});
});
});
#2
1
I think you are closing your HTML in a wrong order, you are closing the Ul and its outer LI inside the each loop
我认为你正在以错误的顺序关闭HTML,你正在关闭每个循环中的UI及其外部LI
#1
1
Try this looks like what you want I hv simulated it here http://jsfiddle.net/d4udts/W9cCE/5/
尝试这看起来像你想要的我在这里模拟它http://jsfiddle.net/d4udts/W9cCE/5/
$(document).ready(function () {
var url = '<%: Url.Content("~/") %>' + "Products/GetMenuList";
$.getJSON(url, function (data) {
var mainMenu = $("#content ul#navmenu-v");
$.each(data, function (index, dataOption) {
var new_li = $("<li class='level1'><a href='javascript:void(0);' id='"+ dataOption.ID + "' class ='selectedcategory'>" + dataOption.Name + "</a>");
mainMenu.append(new_li);
});
$('.level1').hover(function(){
$(this).append("<ul class='subCat'></ul>");
var ul=$(this).children('ul');
var urlCat = '<%: Url.Content("~/") %>' + "Products/GetCategoryList";
$.getJSON(urlCat, { Depid: $(this).find('a.selectedcategory').attr('id')}, function (dataCat) {
$.each(dataCat, function (indexCat, dataOptionCat) {
$(ul).append("<li><a href='javascript:void(0);'" +
"class='selectedsubcat' id='" + dataOptionCat.id + "'>" +
dataOptionCat.Name + "</a></li>");
});
});
},function(){
$(this).children('ul').remove();
});
});
});
#2
1
I think you are closing your HTML in a wrong order, you are closing the Ul and its outer LI inside the each loop
我认为你正在以错误的顺序关闭HTML,你正在关闭每个循环中的UI及其外部LI