用自寫(xiě)的jQuery庫(kù)+Ajax實(shí)現(xiàn)了省市聯(lián)動(dòng)功能(附實(shí)例代碼)
1. 省市聯(lián)動(dòng):
在網(wǎng)頁(yè)上,選擇對(duì)應(yīng)的省份之后,動(dòng)態(tài)的關(guān)聯(lián)出該省份對(duì)應(yīng)的市。選擇對(duì)應(yīng)的市之后,動(dòng)態(tài)地關(guān)聯(lián)出城市對(duì)應(yīng)的區(qū)。
2. 設(shè)計(jì)數(shù)據(jù)庫(kù)表
t_area (區(qū)域表) id(PK-自增) code name pcode --------------------------------------------- 1 001 河北省 null 2 002 河南省 null 3 003 石家莊 001 4 004 邯鄲 001 5 005 鄭州 002 6 006 洛陽(yáng) 002 7 007 江蘇 null 8 008 南京 007 將全國(guó)所有的省、市、區(qū)、縣等信息都存儲(chǔ)到一張表當(dāng)中。 采用的存儲(chǔ)方式實(shí)際上是code pcode形勢(shì)。
3. 這里只是一個(gè)模擬,所以建的數(shù)據(jù)庫(kù)是不完整的,想要完整的數(shù)據(jù)庫(kù),可以去網(wǎng)上找。
4. 上代碼
(1)自寫(xiě)的jQquery庫(kù)
function jQuery(selector){ // selector可能是#id,也可以是其他的選擇器,例如類(lèi)選擇器:.class
if(typeof selector == "string"){
if (selector.charAt(0) == '#') {
domObj = document.getElementById(selector.substring(1));
return new jQuery();
}
}
if(typeof selector == "function"){
window.onload = selector;
}
this.html = function(htmlStr){
domObj.innerHTML = htmlStr;
}
this.click = function(fun){
domObj.onclick = fun;
}
this.val = function(v){
if (v == undefined) {
return domObj.value;
}else{
domObj.value = v;
}
}
this.change = function(fun){
domObj.onchange = fun;
}
// 靜態(tài)的方法:發(fā)送ajax請(qǐng)求
jQuery.ajax = function(jsonArgs){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState == 4) {
if (this.status == 200) {
var jsonObj = JSON.parse(this.responseText);
jsonArgs.success(jsonObj);
}
}
}
if (jsonArgs.type.toUpperCase() == "POST") {
xhr.open("POST",jsonArgs.url,jsonArgs.async);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
xhr.send(jsonArgs.data);
}
if (jsonArgs.type.toUpperCase() == "GET") {
xhr.open("GET",jsonArgs.url + "?" + jsonArgs.data,jsonArgs.async);
xhr.send();
}
}
}
$=jQuery;
(2)html文件(Ajax請(qǐng)求)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用ajax實(shí)現(xiàn)省市聯(lián)動(dòng)</title>
</head>
<body>
<!--引入自己編寫(xiě)的jQuery庫(kù)-->
<script type="text/javascript" src="/ajax/js/jQuery-1.0.0.js"></script>
<!--<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>-->
<script type="text/javascript">
$(function(){
// 發(fā)送ajax請(qǐng)求,獲取所有的省份,省份的pcode是null
$.ajax({
type: "get",
url : "/ajax/listArea",
data : "t=" + new Date().getTime(),
async : true,
success:function(jsonArr){
var html = "<option value=\"\">--請(qǐng)選擇省份--</option>";
for (var i = 0; i < jsonArr.length; i++) {
var area = jsonArr[i];
html += "<option value=\""+area.code+"\">"+area.name+"</option>"
}
$("#province").html(html)
}
})
// 只要change發(fā)生,就發(fā)送ajax請(qǐng)求
$("#province").change(function(){
$.ajax({
type: "get",
url : "/ajax/listArea",
data : "t=" + new Date().getTime()+ "&pcode="+this.value,
async : true,
success:function(jsonArr){
var html = "<option value=\"\">--請(qǐng)選擇市--</option>";
for (var i = 0; i < jsonArr.length; i++) {
var area = jsonArr[i];
html += "<option value=\""+area.code+"\">"+area.name+"</option>"
}
$("#city").html(html)
}
})
})
})
</script>
<select id="province"></select>
<select id="city"></select>
</body>
</html>(3)servlet文件(后端)
package com.bjpowernode.ajax.servlet;
import com.alibaba.fastjson.JSON;
import com.bjpowernode.ajax.bean.Area;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
/**
* 動(dòng)態(tài)獲取所有的省份
*/
@WebServlet("/listArea")
public class ListAreaServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 連接數(shù)據(jù)庫(kù),獲取所有的對(duì)應(yīng)區(qū)域,最終響應(yīng)一個(gè)JSON格式的字符串給WEB前端
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs= null;
ArrayList<Area> areas = new ArrayList<>();
String pcode = request.getParameter("pcode");
String sql;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bjpowernode?useUnicode=true&characterEncoding=UTF-8";
String user = "root";
String password = "1234";
conn = DriverManager.getConnection(url,user,password);
if (pcode == null){
sql = "select code,name from t_area where pcode is null";
ps = conn.prepareStatement(sql);
}else{
sql = "select code,name from t_area where pcode = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,pcode);
}
rs = ps.executeQuery();
while (rs.next()) {
String code = rs.getString("code");
String name = rs.getString("name");
Area area = new Area(code, name);
areas.add(area);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally{
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
response.setContentType("text/html,charset=UTF-8");
String json = JSON.toJSONString(areas);
response.getWriter().print(json);
}
}5. 展示效果


總結(jié)
到此這篇關(guān)于用自寫(xiě)的jQuery庫(kù)+Ajax實(shí)現(xiàn)了省市聯(lián)動(dòng)功能的文章就介紹到這了,更多相關(guān)自寫(xiě)jQuery庫(kù)+Ajax實(shí)現(xiàn)省市聯(lián)動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用AjaxControlToolkit實(shí)現(xiàn)百度搜索時(shí)的下拉列表提示詳細(xì)步驟
AjaxControlToolkit是一組控件的集合,可以實(shí)現(xiàn)自動(dòng)補(bǔ)充文本框,點(diǎn)擊文本框彈出日歷,加水印等Ajax效果等等,感興趣的朋友可以了解下啊,或許本文對(duì)你學(xué)習(xí)ajax有所幫助2013-02-02
Ajax請(qǐng)求二進(jìn)制流進(jìn)行處理(ajax異步下載文件)的簡(jiǎn)單方法
最近做項(xiàng)目遇到這樣的需求:管理后臺(tái)需要隨時(shí)下載數(shù)據(jù)報(bào)表,數(shù)據(jù)要實(shí)時(shí)生成后轉(zhuǎn)換為excel下載。怎么解決這個(gè)問(wèn)題呢?下面小編給大家分享Ajax請(qǐng)求二進(jìn)制流進(jìn)行處理(ajax異步下載文件)的簡(jiǎn)單方法,一起看看吧2017-09-09
Ajax的原生實(shí)現(xiàn)關(guān)于MIME類(lèi)型的使用方法
下面小編就為大家分享一篇Ajax的原生實(shí)現(xiàn)關(guān)于MIME類(lèi)型的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Ajax與mysql數(shù)據(jù)交互制作留言板功能(全)
這篇文章主要為大家詳細(xì)介紹了Ajax與mysql數(shù)據(jù)交互,實(shí)現(xiàn)留言板功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
解析ajax核心XMLHTTPRequest對(duì)象的創(chuàng)建與瀏覽器的兼容問(wèn)題
這篇文章主要介紹了ajax核心XMLHTTPRequest對(duì)象的創(chuàng)建與瀏覽器的兼容問(wèn)題。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
jquery ajax實(shí)現(xiàn)批量刪除具體思路及代碼
回調(diào)函數(shù),在請(qǐng)求完成后需要進(jìn)行的操作:此處是把選中的checkbox去掉,接下來(lái)為大家詳細(xì)介紹下,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04

