简体   繁体   English

使用JavaScript而不使用数据库的动态下拉菜单

[英]Dynamic drop down menus using JavaScript without using a database

Is there a quick and dirty technique of creating dynamic drop down menus without using a database backend? 是否有不使用数据库后端创建动态下拉菜单的快速而肮脏的技术? There will be three levels of drop down menus and there are an awful lot of items in them, hence the quick and dirty! 下拉菜单将分为三个级别,其中包含很多项目,因此又快又脏!

Thanks 谢谢

The fact that you choose to load the items from a database is pretty much unrelated to the drop-down menu itself. 您选择从数据库加载项目的事实与下拉菜单本身几乎没有关系。 You can always read from a JSON, .csv or xml file. 您始终可以从JSON,.csv或xml文件读取。 Depending on the implementation, you could even have the items loaded straight into the HTML page (but that would be painfully slow if you have many of them, so not recommended). 根据实现的不同,您甚至可以直接将这些项目加载到HTML页面中(但是如果其中有很多项目,这样做会很慢,因此不建议这样做)。

Load the file (preferably JSON) containing your items via AJAX, parse it and create the markup dynamically. 通过AJAX加载包含项的文件(最好是JSON),对其进行解析并动态创建标记。

It's pretty hard to give details as the source of the contents shouldn't be so important. 提供细节非常困难,因为内容的来源不应该那么重要。 If you plan to use the classic <select> tag, just create that element. 如果您打算使用经典的<select>标签,只需创建该元素。 jQuery is your friend for quick and dirty. jQuery是您快速又肮脏的朋友。

Using JSON.parse() will return the structured object which you can use to loop through elements and add children to the <select> node. 使用JSON.parse()将返回结构化对象,您可以使用该结构化对象遍历元素并将子级添加到<select>节点。

Your question is a bit vague, so I'll give you a "general" answer. 您的问题有点含糊,所以我给您一个“一般”的答案。

There are tools like jQuery and knockoutJS out there. 那里有类似jQuery和kickoutJS的工具。 You could use knockoutJS to represent a client side view model and use it ( maybe combined with jQuery ) to populate the view with your data. 您可以使用敲除JS来表示客户端视图模型,并使用它(可能与jQuery结合使用)以用数据填充视图。

I think you should take a look at knockoutJS and especially observables, observable arrays, the options binding, and its support for AJAX requests - that sounds like a reasonable place to start. 我认为您应该看一下基因敲除JS,尤其是可观察的,可观察的数组, options绑定及其对AJAX请求的支持-听起来像是一个合理的起点。

The knockoutJS documentation can be found here: http://knockoutjs.com/documentation/introduction.html 您可以在此处找到基因敲除JS文档: http : //knockoutjs.com/documentation/introduction.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM