JQ完成省市联动(JSON)

编程入门 行业动态 更新时间:2024-10-28 11:22:04

JQ完成<a href=https://www.elefans.com/category/jswz/34/1751175.html style=省市联动(JSON)"/>

JQ完成省市联动(JSON)

1.引入的包


2.代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>注册页面</title><meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript">function checkusername() {var username = document.getElementById("username").value;//创建异步对象var xh = new XMLHttpRequest();//设置状态改变的监听xh.onreadystatechange = function() {if (xh.readyState == 4) {//请求发送成功if (xh.status == 200) {//响应成功var data = xh.responseText;if (data == 1) {document.getElementById("s1").innerHTML = "用户名可以使用";document.getElementById("sub").disabled = false;} else if (data == 2) {document.getElementById("s1").innerHTML = "用户名已经被注册了";document.getElementById("sub").disabled = true;}}}}xh.open("GET", "/J2EE/usernamecheckservlet?username=" + username, true);xh.send(null);}
</script>
<script type="text/javascript" src="/J2EE/html/jquery-3.2.1.js"></script>
<script type="text/javascript">$(function() {$("#username").blur(function() {var username = $("#username").val();//演示get/post方法$.get(處理的servlet,传给servlet的map,回调函数(servlet:response.getWriter().println(1)的值))$.get("/J2EE/usernamecheckservlet", {"username" : username}, function(data) {if (data == 1) {$("#s1").html("用户名可以使用");$("#sub").attr("disabled", false);//改标签的属性值} else if (data == 2) {$("#s1").html("用户名已经被注册了");$("#sub").attr("disabled", true);}});});});
</script>
<!-- <script type="text/javascript">$(function() {//为省份的下拉列表绑定事件$("#pro").change(function() {var pid = $(this).val();$.post("/J2EE/searchcity", {"pid" : pid}, function(data) {$(data).find("city").each(function() {/* 通过find方法查找所以的city元素,并通过each进行遍历 */var cid = $(this).children("cid").text();/* 当前city的子元素cid的文本内容 */var cname = $(this).children("cname").text();$("#city").append("<option value='"+cid+"'>" + cname+ "</option>");});});});});
</script> -->
<script type="text/javascript">$(function() {//为省份的下拉列表绑定事件$("#pro").change(function() {var pid = $(this).val();$.post("/J2EE/jasonservlet", {"pid" : pid}, function(data) {var $city = $("#city");$city.html("<option>--请选择--</option>");//清空$(data).each(function(i, n) {$city.append("<option value='"+n.cid+"'>"+ name + "</option>");});}, "json");//JS识别json});});
</script>
<style type="text/css" contenteditable="inherit">
div {width: 100%;
}#bodydiv {width: 90% px;
}.logodiv {width: 33%;float: left;height: 48px;
}.clear {clear: both;
}.cd {background-color: black;height: 30px;
}
</style></head><body><div id="bodydiv"><div class="logodiv"><img alt="" src="/J2EE/imag/head1.png" height=100%></div><div class="logodiv"><img alt="" src="/J2EE/imag/head2.png" height=100%></div><div class="logodiv"style="padding-top: 5px; text-align: center; height: 43px"align="center"><a href="#">登陆</a> <a href="zhuce.html">注册</a> <a href="#">购物车</a></div><div class="clear"></div><div class="cd"><a href="shouyediv.html">首页</a> <a href="#">手机数码</a> <a href="#">电脑办公</a><a href="#">研烟酒箱包</a></div><divstyle="height: 800px; background-image: url('/J2EE/imag/zhuce.jpg'); background-size: 100% 100%;"><div style=" height:90%;position: absolute;top: 300px;" align="left"><form method="post" action="/J2EE/registservlet"><table align="center" border="0" width=40%><tr><td align="right" width=50%>用户名    <td align="left"><input class="required" type="text"name="username" id="username"></input><span id="s1"></span></td></tr><tr><td align="right" width=50%>密码    <td align="left"><input class="required" type="password"name="password" id="password"></input></tr><tr><td align="right" width=50%>确认密码    <td align="left"><input class="required" type="password"name="repassword"></input><span id="repassword"></span></td></tr><tr><td align="right" width=50%>邮箱    <td align="left"><input type="text" name="email"></input></td></tr><tr><td align="right" width=50%>性别    <td align="left"><input type="radio" name="sex" value="man"checked="checked">男<input type="radio" name="sex"value="woman">女</td></tr><tr><td align="right" width=50%>生日    <td align="left"><input type="date" name="birth"></td></tr><tr><td align="right" width=50%>爱好    <td align="left"><input type="checkbox" name="love"value="1">看电视<input type="checkbox" name="love"value="2">打游戏<input type="checkbox" name="love"value="3">吃饭<input type="checkbox" name="love" value="4"checked>睡觉</td></tr><tr><td align="right" width=50%>籍贯    <td align="left"><select id="pro" name="pro"><option>--请选择--</option><c:forEach var="p" items="${province}"><option value="${p.pid}">${p.pname}</option></c:forEach></select> <select name="city" id="city"><option>--请选择--</select><tr><td align="right" width=50%>头像    <td align="left"><input type="file"></td></tr><tr><td align="right" width=50%>个人简介    <td align="left"><textarea cols="" rows="5" name="a">我是一个</textarea></td></tr><tr><td colspan="2" align="center"><input type="submit"value="注册  " id="sub"><input type="reset" value="重置"></td></tr></table></form></div></div><div><img alt="" src="../imag/q.png" width="100%"></div><div style="text-align: center;"><a href="html.html">关于我们</a>  公司简介  招贤纳士  广告服务  联系方式  合作伙伴  论坛反馈<br>版权声明:Copyright© 1999-2016,赵瑾的网站 , All Rights Reserved</div></div>
</body>
</html>

package regist;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;public class jasonservlet extends HttpServlet {/** 相应json的JQ案例;跳转到注册页面* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String pid=request.getParameter("pid");provinceservice ps=new provinceservice();try {List<city>cities=ps.searchcity(pid);//不想要的属性删除JsonConfig jc=new JsonConfig();jc.setExcludes(new String[]{"pid"});//将list集合转为jsonJSONArray ja = JSONArray.fromObject(cities,jc);response.setContentType("text/html;charset=UTF-8");response.getWriter().println(ja.toString());} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}


更多推荐

JQ完成省市联动(JSON)

本文发布于:2024-02-17 14:16:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1694296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:省市   JQ   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!