gpt4 book ai didi

javascript - 从 jQuery AJAX 动态创建 Bootstrap 下拉列表不起作用

转载 作者:行者123 更新时间:2023-11-30 12:46:35 25 4
gpt4 key购买 nike

我是 jQuery 的新手,使用它已经 3 天了,在提问之前我已经查看了这里的所有问题。我想使用 json 文件中的数据用下拉菜单填充导航栏,我能够完美地解析数据,但是用于 Bootstrap 下拉菜单的用法,我正在动态添加来自 jQuery 的类,但它们根本不起作用,请帮忙。

这是我的 index.html

    <html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<title>JavaScipt AJAX</title>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div class="navbar secondary-navbar" id="department-nav">
<div class="navbar-inner" id="menu">

</div>
</div>
<script src="jquery.min.js"></script>
<script src="script_menu.js"></script>
<script src="bootstrap.min.js"></script>
</body>
</html>

sript_menu.js

$( document ).ready(function() {
$.getJSON( "data.json", function(data) {
console.log( data );
var menu_item = '<ul class="nav nav-departments nav-featured">';
var output = '<ul class="nav-list">';
$.each(data, function(key, val) {
if(val.warehouse_id=="1"){
if(val.name=="Produce" || val.name=="Bakery" || val.name=="Dairy & Eggs" || val.name=="Beverages" ) {
menu_item += '<li class="dropdown">';
menu_item += '<a class="navbar-link dropdown-toggle">' + val.name + '</a>';

output+= '<li>';
output+= '<h2>' + val.name + '</h2>';

menu_item += '<ul class="dropdown-menu">';
for(var i=0; i<val.aisles.length; i++) {
output+= '<p>' + val.aisles[i].name + '</p>';
menu_item += '<li>' + val.aisles[i].name + '</li>';
}
menu_item += '</ul>';
menu_item += '</li>';
output+= '</li>';
}

}
});
output+= '</ul>';
menu_item+= '</ul>';

$('#menu').html(menu_item);
$('#update').html(output);
});
});

这是我的 data.json

[

{
"display_name": "Beer, Wine & Spirits",
"id": 148,
"name": "Beer, Wine & Spirits",
"rank_offset": 991,
"sort_order": 9,
"visible": true,
"warehouse_id": 2,
"aisles": [{
"department_id": 148,
"id": 979,
"name": "Spirits",
"rank_offset": 1,
"visible": true
}, {
"department_id": 148,
"id": 976,
"name": "Beer",
"rank_offset": 2,
"visible": true
}]
}, {
"display_name": "Prepared Food",
"id": 59,
"name": "Prepared Food",
"rank_offset": 990,
"sort_order": 3,
"visible": true,
"warehouse_id": 2,
"aisles": [{
"department_id": 59,
"id": 776,
"name": "Tofu & Soy",
"rank_offset": 45,
"visible": true
}, {
"department_id": 59,
"id": 465,
"name": "Salads, Sushi & Wraps",
"rank_offset": 10,
"visible": true
}, {
"department_id": 59,
"id": 466,
"name": "Sides & Soups",
"rank_offset": 40,
"visible": true
}, {
"department_id": 59,
"id": 468,
"name": "Whole Meals",
"rank_offset": 20,
"visible": true
}, {
"department_id": 59,
"id": 764,
"name": "Fresh Pasta",
"rank_offset": 35,
"visible": true
}, {
"department_id": 59,
"id": 770,
"name": "Desserts",
"rank_offset": 50,
"visible": true
}, {
"department_id": 59,
"id": 771,
"name": "Fresh Pizza",
"rank_offset": 30,
"visible": true
}]
}, {
"display_name": "Grocery",
"id": 57,
"name": "Grocery",
"rank_offset": 997,
"sort_order": 5,
"visible": true,
"warehouse_id": 2,
"aisles": [{
"department_id": 57,
"id": 443,
"name": "Spices",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 444,
"name": "Baking",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 445,
"name": "Condiments",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 446,
"name": "Peanut Butter & Jelly",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 447,
"name": "Oil, Vinegar & Tuna",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 448,
"name": "Oil & Olives",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 449,
"name": "Canned & Jarred Vegetables",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 450,
"name": "Tomato Sauce",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 451,
"name": "Pasta",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 452,
"name": "Grains, Rice & Dried Beans",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 453,
"name": "Soup",
"rank_offset": 100,
"visible": true
}]
}, {
"display_name": "Personal & Home Care",
"id": 56,
"name": "Personal & Home Care",
"rank_offset": 994,
"sort_order": 6,
"visible": true,
"warehouse_id": 2,
"aisles": [{
"department_id": 56,
"id": 435,
"name": "Vitamins & Supplements",
"rank_offset": 30,
"visible": true
}, {
"department_id": 56,
"id": 436,
"name": "Paper Products",
"rank_offset": 20,
"visible": true
}, {
"department_id": 56,
"id": 438,
"name": "Pets!",
"rank_offset": 40,
"visible": true
}, {
"department_id": 56,
"id": 437,
"name": "Cleaning Products",
"rank_offset": 10,
"visible": true
}, {
"department_id": 56,
"id": 778,
"name": "Personal Care",
"rank_offset": 28,
"visible": true
}, {
"department_id": 56,
"id": 779,
"name": "Soap",
"rank_offset": 25,
"visible": true
}]
}
]

感谢您的耐心等待..

最佳答案

您无法在加载文档后仅使用类名来初始化 Bootstrap 组件。当您将类名添加到组件时,Bootstrap 会在页面加载时初始化它们。如果您在页面加载后添加组件,您需要使用 Bootstrap 文档中列出的“通过 JavaScript”初始化方法自行初始化它,即对于下拉列表,您需要触发 $('.dropdown-toggle ').dropdown() ( Bootstrap docs )

关于javascript - 从 jQuery AJAX 动态创建 Bootstrap 下拉列表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com