jQuery jsp下拉方塊聯動取得本機資料的方法(附原始碼)_jquery
本文實例講述了jQuery jsp下拉框聯動獲取本地資料的方法。分享給大家參考,具體如下:
JQuery下拉框連動很好的體現了Ajax的按需取資料的要求,減少資料的互動量。 (點此下載原始碼)
下面的實例使用Json將伺服器端的類別或物件轉換為JSON格式,主要運用了6個jar套件
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
ezmorph-1.0.3.jar
json-lib-2.1.jar
下面貼上實驗圖,並詳細解說主要程式碼
顯示頁index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>JQuery实例-级联下拉框效果</title> <meta http-equiv= "Content-Type" content="text/html";charset=UTF-8"> <link type="text/css" rel="stylesheet" href="css/chainselect.css" /> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/chainselect.js"></script> </head> <body> <div class="loading"> <p><img src="/static/imghw/default1.png" data-src="images/data-loading.gif" class="lazy" alt="jQuery jsp下拉方塊聯動取得本機資料的方法(附原始碼)_jquery" /></p> <p>jQuery jsp下拉方塊聯動取得本機資料的方法(附原始碼)_jquery......</p> </div> <div class="car"> <span class="carname"> 汽车厂商: <select> <option value="" selected="selected">请选择汽车厂商</option> <option value="BMW">宝马</option> <option value="Audi">奥迪</option> <option value="VW">大众</option> </select> <img src="/static/imghw/default1.png" data-src="images/pfeil.gif" class="lazy" alt="有数据" /> </span> <span class="cartype"> 汽车类型: <select></select> <img src="/static/imghw/default1.png" data-src="images/pfeil.gif" class="lazy" alt="有数据" /> </span> <span class="wheeltype"> 车轮类型: <select></select> </span> </div> <div class="carimage"> <p><img class="carloading lazy" src="/static/imghw/default1.png" data-src="images/img-loading.gif" alt="图片装载中" /></p> <p><img class="carimg lazy" src="/static/imghw/default1.png" data-src="images/BMW_316ti_rha.jpg" alt="汽车图片"/></p> </div> </body> </html>
修飾檔案chainselect.css
.loading { width: 400px; /*margin-left: auto;*/ /*margin-right: auto;*/ margin: 0 auto; visibility: hidden; } .loading p { text-align: center; } p { margin: 0; } .car { /*width: 500px;*/ /*margin: 0 auto;*/ text-align: center; } .carimage { text-align: center; } .cartype, .wheeltype, .carloading, .carimg, .car img { display: none; }
在這裡,要注意屬性 display: none; 與 visibility: hidden;的差別
display: none;
使用該屬性後,HTML元素(物件)的寬度、高度等各種屬性值都會「遺失」;
visibility: hidden;
使用該屬性後,HTML元素(物件)只是在視覺上看不見(完全透明),而它所佔據的空間位置仍然存在,也即是說它仍具有高度、寬度等屬性值。
JQUERY處理文件chainselect.js
$(document).ready(function(){ //找到三个下拉框 var carnameSelect = $(".carname").children("select"); var cartypeSelect = $(".cartype").children("select"); var wheeltypeSelect = $(".wheeltype").children("select"); var carimg = $(".carimg"); //给三个下拉框注册事件 carnameSelect.change(function(){ //1.需要获得当前下拉框的值 var carnameValue = $(this).val(); //1.1只要第一个下拉框内容有变化,第三个下拉框都要先隐藏起来 wheeltypeSelect.parent().hide(); //1.2将汽车图片隐藏起来 carimg.hide().attr("src",""); //2.如果值不为空,则将下拉框的值传送给服务器 if (carnameValue != "") { if (!carnameSelect.data(carnameValue)) { //对应服务器端程序 CarJsonServlet的属性,并将该Servlet中的数据转换为JSON格式 $.post("CarJsonServlet",{keyword: carnameValue, type: "top"},function(data){ //2.1接收服务器返回的汽车类型 ,data为数组格式 if (data.length != 0) { //2.2解析汽车类型的数据,填充到汽车类型的下拉框中 cartypeSelect.html(""); $("<option value=''>请选择汽车类型</option>").appendTo(cartypeSelect); for (var i = 0; i < data.length; i++) { $("<option value='" + data[i] + "'>" + data[i] + "</option>").appendTo(cartypeSelect); } //2.2.1汽车类型的下拉框显示出 cartypeSelect.parent().show(); //2.2.2第一个下拉框后面的指示图片显示出来 carnameSelect.next().show(); } else { //2.3没有任何汽车类型的数据 cartypeSelect.parent().hide(); carnameSelect.next().hide(); } carnameSelect.data(carnameValue, data); }, "json"); } } else { //3.如果值为空,那么第二个下拉框所在span要隐藏起来,第一个下拉框后面的指示图片也要隐藏 cartypeSelect.parent().hide(); carnameSelect.next().hide(); } }); cartypeSelect.change(function(){ //1.需要获得当前下拉框的值 var cartypeValue = $(this).val(); //1.1将汽车图片隐藏起来 carimg.hide().attr("src",""); //2.如果值不为空,则将下拉框的值传送给服务器 if (cartypeValue != "") { if (!cartypeSelect.data(cartypeValue)) { $.post("CarJsonServlet",{keyword: cartypeValue, type: "sub"},function(data){ //2.1接收服务器返回的汽车类型 if (data.length != 0) { //2.2解析汽车类型的数据,填充到车轮类型的下拉框中 wheeltypeSelect.html(""); $("<option value=''>请选择车轮类型</option>").appendTo(wheeltypeSelect); for (var i = 0; i < data.length; i++) { $("<option value='" + data[i] + "'>" + data[i] + "</option>").appendTo(wheeltypeSelect); } //2.2.1车轮类型的下拉框显示出 wheeltypeSelect.parent().show(); //2.2.2第二个下拉框后面的指示图片显示出来 cartypeSelect.next().show(); } else { //2.3没有任何汽车类型的数据 wheeltypeSelect.parent().hide(); cartypeSelect.next().hide(); } cartypeSelect.data(cartypeValue, data); }, "json"); } } else { //3.如果值为空,那么第三个下拉框所在span要隐藏起来,第二个下拉框后面的指示图片也要隐藏 wheeltypeSelect.parent().hide(); cartypeSelect.next().hide(); } }); wheeltypeSelect.change(function(){ //1.获取车轮类型 var wheeltypeValue = $(this).val(); //2.根据汽车厂商名称,汽车型号和车轮类型得到汽车图片的文件名 var carnameValue = carnameSelect.val(); var cartypeValue = cartypeSelect.val(); var carimgname = carnameValue + "_" + cartypeValue + "_" + wheeltypeValue + ".jpg"; //3.显示出loading的图片 carimg.hide(); $(".carloading").show(); //4.通过Javascript中的Image对象预装载图片 var cacheimg = new Image(); $(cacheimg).attr("src","images/" + carimgname).load(function(){ //隐藏loading图片 $(".carloading").hide(); //显示汽车图片 carimg.attr("src","images/" + carimgname).show(); }); //3.修改汽车图片节点的src的值,让汽车图片显示出来 //carimg.attr("src","images/" + carimgname).show(); //4.使汽车图片的节点显示出来 }); //给jQuery jsp下拉方塊聯動取得本機資料的方法(附原始碼)_jquery的节点定义ajax事件,实现动画提示效果 $(".loading").ajaxStart(function(){ $(this).css("visibility","visible"); $(this).animate({ opacity: 1 },0); }).ajaxStop(function(){ $(this).animate({ opacity: 0 },500); }); })
問題? ? ? :$("").appendTo(cartypeSelect);這裡出現中文亂碼怎麼解決? ? ?
伺服器端CarJsonServlet
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; public class CarJsonServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解决中文乱码 response.setHeader("Cache-Control", "no-cache"); response.setContentType("text/json;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); //得到type,keyword的值 String type = request.getParameter("type"); String keyword = request.getParameter("keyword"); JSONArray jsonArrayResult = new JSONArray(); if ("top".equals(type)) { if ("BMW".equals(keyword)) { jsonArrayResult.add("316ti"); jsonArrayResult.add("6ercupe"); } else if ("Audi".equals(keyword)) { jsonArrayResult.add("tt"); } else if ("VW".equals(keyword)) { jsonArrayResult.add("Golf4"); } } else if ("sub".equals(type)) { if ("tt".equals(keyword)) { jsonArrayResult.add("rha"); jsonArrayResult.add("rhb"); jsonArrayResult.add("rhc"); } else if ("316ti".equals(keyword)) { jsonArrayResult.add("rha"); jsonArrayResult.add("rhb"); } else if ("6ercupe".equals(keyword)) { jsonArrayResult.add("rha"); jsonArrayResult.add("rhb"); jsonArrayResult.add("rhc"); } else if ("Golf4".equals(keyword)) { jsonArrayResult.add("rha"); jsonArrayResult.add("rhb"); } } PrintWriter out = response.getWriter(); out.write(jsonArrayResult.toString()); out.flush(); out.close(); } }
設定檔web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <servlet> <servlet-name>CarJsonServlet</servlet-name> <servlet-class>CarJsonServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CarJsonServlet</servlet-name> <url-pattern>/CarJsonServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
JQuery and other development knowledge learned in this section:
1. The alt attribute of the img tag should be written. When the image has not been loaded or the image does not exist, the text information of this attribute will be displayed
2.select represents a drop-down box. Each item in the drop-down box is an option. The content in the option start and end tags will be displayed on the page. The value of the value attribute is used to obtain and send to the server using the val method in JQuery. of. When the attribute value of selected is defined as selected, it means that the current option is selected
3. How to display the div element in the center: set the width of the div, and then the values of margin-left and margin-right are both auto. The abbreviation is margin: 0 auto;
4. The p tag of html represents the content of a paragraph. The content will occupy one or more lines, and the following content will be displayed on another line
5. In order to center the text and pictures in p, you can use the text-align attribute, and the attribute value is center. The p tag has margin-top and margin-bottom values by default, which can be cleared through css if necessary
6. When the visibility attribute value is hidden, the element is hidden, but unlike display, which is none, it still occupies a certain space on the page, but
is not displayed.
7. If multiple selectors have the same attribute value, they can be defined together and separated by commas
8. The change method corresponds to the onchange event in standard Javascript and can handle events when the content of the drop-down box changes
9.The parent method can obtain the parent node of a node
10. The next method can get the next sibling node of a node, and the corresponding previous method can get the previous sibling node of a node
11. The $.post method can initiate an asynchronous post request with the server. The first parameter is the server-side address of the request, the second parameter is the data sent to the server, the parameter is a Javascript object, expressed in the form of name-value pairs, the third parameter is the callback method, and the fourth parameter indicates the server The data type returned by the end, JQuery will help us convert according to this parameter. The get method only differs in the second parameter, and the other parameters have the same usage
12.The simple object definition method in Javascript is {key1: value1, key2: value2}
13. The data format of JSON is actually the text format content defined by an object or data in Javascript, such as {key1: value1, key2: [1,2,3]} or [1,2,{key2:
value2}]
14. You can directly use the $("") method to create the options in the drop-down box, and then use the appendTo method to add them to the drop-down box
15. The attr method can set or get the attribute value of a node
16.ajaxStart is executed before each ajax request issued by JQuery starts, ajaxStop is executed after all ajax requests in the JQuery queue are completed, and ajaxComplete is executed after each ajax request issued by JQuery is completed
17. fadeOut and fadeIn can achieve the effect of fade out and fade in. The parameter content is similar to the slideUp and slideDown methods.
18. The animate method can achieve any animation effect and can control a certain css attribute to change within a certain period of time to achieve the animation effect
19.Opacity can change the transparency of elements. In IE, a filter is used to implement it. 100 means fully displayed and 0 means completely transparent. Non-IE browsers use the opacity attribute. 1 means fully displayed and 0 means transparent. JQuery blocks browser differences in the animate method, and you can use opacity directly to achieve the fade-in and fade-out effect.
20. The data method can be used to cache data. Caching can improve the operating efficiency of the system and reduce the load on the server
21. You can use the Image object in Javascript to preload images, so that you can know when the image is loaded, so that you can give some prompt information for image loading.
22. The load method can correspond to the onload event in Javascript. In this example, it is used to capture the event that the image is loaded
I hope this article will be helpful to everyone in jQuery programming.

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

jQuery引用方法詳解:快速上手指南jQuery是一個受歡迎的JavaScript庫,被廣泛用於網站開發中,它簡化了JavaScript編程,並為開發者提供了豐富的功能和特性。本文將詳細介紹jQuery的引用方法,並提供具體的程式碼範例,幫助讀者快速上手。引入jQuery首先,我們需要在HTML檔案中引入jQuery函式庫。可以透過CDN連結的方式引入,也可以下載

jQuery如何移除元素的height屬性?在前端開發中,經常會遇到需要操作元素的高度屬性的需求。有時候,我們可能需要動態改變元素的高度,而有時候又需要移除元素的高度屬性。本文將介紹如何使用jQuery來移除元素的高度屬性,並提供具體的程式碼範例。在使用jQuery操作高度屬性之前,我們首先需要了解CSS中的height屬性。 height屬性用於設定元素的高度

jQuery中如何使用PUT請求方式?在jQuery中,發送PUT請求的方法與發送其他類型的請求類似,但需要注意一些細節和參數設定。 PUT請求通常用於更新資源,例如更新資料庫中的資料或更新伺服器上的檔案。以下是在jQuery中使用PUT請求方式的具體程式碼範例。首先,確保引入了jQuery庫文件,然後可以透過以下方式發送PUT請求:$.ajax({u

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:<

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

實現步驟:1、在JSP頁面中引入JSTL標籤庫;2、從資料庫取得資料;3、對資料進行分頁處理;4、在頁面中顯示分頁導覽條;5、根據目前頁碼和每頁顯示數量,從分頁後的資料中取得對應的資料並顯示在頁面上即可。

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬

JSP能否被PHP取代? JSP(JavaServerPages)和PHP(HypertextPreprocessor)都是常用的伺服器端腳本語言,用於動態產生網頁內容。雖然它們各有特色和優勢,但在實際應用中,是否可以完全取代對方?本文將分析兩者的優缺點,並透過具體的程式碼範例進行比較。首先,我們來看看JSP和PHP各自的特色。 JSP是一種基於Java的
