This article mainly introduces the cascade operation of the drop-down menu in detail, and shares the tool class for interactive transmission of data between ajax and the background. Interested friends can refer to
often used in development You will encounter cascading menu operations, such as: selection of countries, cities, towns, etc. When a country is selected, the following menu will list the cities in that country. When a city is selected, the following menu will list the corresponding towns.
There are two ways I understand to solve the cascading operation of this kind of menu:
① Use js to implement it, and put the cascading data used on the page into js Within, when the page is loaded, it is displayed in the corresponding select through js. There are many solutions to this method. The most intuitive one is to put it into a multi-dimensional array. Everyone has different thinking, so I won’t go into details here. Commentary.
② Use ajax to load asynchronously and dynamically, and then display it in the corresponding select. This method is very convenient and is recommended for use in development.
Let’s look at a small example in development:
JSP short page:
<p class="form-group"> <label class="col-sm-2 control-label">设备类别</label> <p class="col-sm-4"> <select class="basic-single" name="codeCategory" onchange="showCodeSubCate()" id="codeCategory" style="width: 100%"> </select> </p> <label class="col-sm-2 control-label">设备子类</label> <p class="col-sm-4"> <select class="basic-single" name="codeSubCategory" id="codeSubCate" disabled="disabled" style="width: 100%"> <option value="">--请选择--</option> </select> </p> </p>
This page involves two options: device classification and device subcategory. The device classification is the first-level menu, and the device subcategory is the second-level menu. The display content of the device subcategory is determined by the device classification.
Let’s look at the ajax code snippet:
function addCodeCategory(){ $.ajax({ url: "<%=request.getContextPath()%>/facilitydict/showCodeCategory", async: false, //请求是否异步,默认为异步,这也是ajax重要特性 type: "GET", //请求方式 success: function(result) { result = $.parseJSON(result); var data = result.data; var codeCates = data.split(","); str ='<option value="6801">--请选择--</option>'; for(x in codeCates){ str+='<option value="'+codeCates[x]+'">'+codeCates[x]+'</option>'; } $("#codeCategory").html(str); } }); } function showCodeSubCate(){ $("#codeSubCate").prop("disabled","");//将设备子类的select解除锁定 var target = $("#codeCategory option:selected").text(); $.ajax({ url: "<%=request.getContextPath()%>/facilitydict/showCodeSubCategory", data : {codeCategory:target}, async: false, //请求是否异步,默认为异步,这也是ajax重要特性 type: "GET", //请求方式 success: function(result) { result = $.parseJSON(result); var data = result.data; var codeCates = data.split(","); var str=""; for(x in codeCates){ str+='<option value="'+codeCates[x]+'">'+codeCates[x]+'</option>'; } $("#codeSubCate").html(str); } }); }
It is not difficult to see that when the content in the device category selector After the change occurs, the showCodeSubCate function is triggered to request the background to obtain data, and then the requested data is added to the select corresponding to the device subclass. The implementation of the background code is as follows (only the controller methods are posted):
@RequestMapping("/showCodeCategory") @ResponseBody public Result<String> searchCodeCategory() { Result<String> rs = new Result<>(); List<String> codeCategorys = facilityDictService.searchCodeCategory(); String codeCate = StringUtil.collectionToCommaDelimitedString(codeCategorys); rs.setData(codeCate); return rs; } @RequestMapping("/showCodeSubCategory") @ResponseBody public Result<String> searchCodeSubCategory(HttpServletRequest request) { String codeCategory = request.getParameter("codeCategory"); Result<String> rs = new Result<>(); List<String> codeSubCategorys = facilityDictService.searchCodeSubCategory(codeCategory); String codeCate = StringUtil.collectionToCommaDelimitedString(codeSubCategorys); rs.setData(codeCate); return rs; }
These two methods respectively correspond to the two ajax requests above, which is worth What is introduced is the data returned by the background. The return value type is Result
Ajax tool class for interacting with the background to transmit data
public class Result<T> implements Serializable { private static final long serialVersionUID = 3637122497350396679L; private boolean success; private T data; private String msg; public Result() { } public Result(boolean success) { this.success = success; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public T getData() { return data; } public void setData(T data) { this.data = data; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Result(boolean success, String msg) { super(); this.success = success; this.msg = msg; } public Result(boolean success, T data) { super(); this.success = success; this.data = data; } }
This class provides a very large platform for front-end and back-end interaction Convenience:
The following is the ajax interaction between the front and backend:
The front-end ajax code:
$.ajax({ url: "<%=request.getContextPath()%>/supp/deleteSupp", data : {supplierId:supplierId}, async: false, //请求是否异步,默认为异步,这也是ajax重要特性 type: "GET", //请求方式 success: function(data) { var rs = eval('('+data+')'); flag = rs.success; if(flag){ alert("删除成功!"); } } });
The following is the background java code:
@RequestMapping("/deleteSupp") @ResponseBody public Result<String> deleteSupplier(HttpServletRequest request){ Result<String> rs = new Result<>(); String supplierId = request.getParameter("supplierId"); supplierService.deleteSupplierById(supplierId); rs.setSuccess(true); return rs; }
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
AJAX display loading and pop-up layer occlusion page implementation example
Ajax submit Form form The page will still be refreshed. A quick solution to the problem
The above is the detailed content of Cascading operations for drop-down menus. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor
