十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
//:ComboBoxText.java
专注于为中小企业提供做网站、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业东山免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class ComboBoxText {
private String[][] xxx = {
{"浙江","杭州","温州","绍兴","舟山",},
{"江苏","南京","无锡","徐州","蒙眬",},
{"上海","闵行区","普陀区","金山区",},
};
private Object[] a(){
Object[] os = new Object[xxx.length];
for(int i=0; ixxx.length; i++){
os[i]=xxx[i][0];
}
return os;
}
private Object[] b(Object a){
for(int i=0; ixxx.length; i++){
if(xxx[i][0].equals(a)){
Object[] o = new Object[xxx[i].length-1];
for(int j=0; jo.length; j++)
o[j]=xxx[i][j+1];
return o;
}
}
return new Object[]{};
}
public static void main(String[] args) {
final ComboBoxText ct = new ComboBoxText();
final JComboBox ca = new JComboBox(ct.a());
final JComboBox cb = new JComboBox();
ca.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
cb.removeAllItems();
Object selected = ca.getSelectedItem();
Object[] newItems = ct.b(selected);
for(int i=0; inewItems.length; i++)
cb.addItem(newItems[i]);
}
});
ca.setSelectedIndex(-1);
JFrame f = new JFrame();f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
f.add(ca);f.add(cb);f.setVisible(true);
f.setSize(200,100);
}
}
这里也只能是给你个概念,要是要真的去做,就必须要有实例来做。首先AJAX简单说就是用一个网页刷新另一个网页的其中的某些部分(例如div标签),AJAX是异步传输的,也就是当你点击一个按钮后,不需要跳转整个页面,AJAX会帮你把你提交的数据发到服务器端,验证或运算完后返回你操作的页面,并局部刷新页面。至于3级联动,我想你说的应该是你点了一下某个控件,第二个控件和第三个控件会随之改变内容,这样的话,也就是点第一个控件的时候会发送数据到服务器,等运算完后返回后刷新第2第3个控件的内容……
以省,市,城联动为例:
此小程序的功能主要是采用异步请求方式从数据库中调取省市区信息显示到下拉列表:
代码如下:
建立数据库中的代码和一些配置文件信息就省略了,主要有JavaScript中的代码为:
$(document).ready(function(){
$.get("getProvince.do", function(result){
$("#showp").html(result);
});
})
var xmlhttp;
function mysend(str){
$(document).ready(function(){
$("#show2").html("");
})
var show = document.getElementByIdx_x_x_x_x_x_x_x_x_x("show");
show.innerHTML = "";
var province = document.getElementByIdx_x_x_x_x_x_x_x_x_x("province").value;
if(province!=0){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 xmlhttp.status==200){
show.innerHTML = xmlhttp.responseText;
}
}
var ss = encodeURIComponent(str);
xmlhttp.open("GET","getCity.do?provinceid="+ss,true);
xmlhttp.send(null);
}
}
function myarea(str){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 xmlhttp.status==200){
var show2 = document.getElementByIdx_x_x_x_x_x_x_x_x_x("show2");
show2.innerHTML = xmlhttp.responseText;
}
}
var ss = encodeURIComponent(str);
xmlhttp.open("GET","getArea.do?cityid="+ss,true);
xmlhttp.send(null);
}
html页面中的代码为:
所在地
action中的代码为:
package mobi.zhangsheng.jiejia.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import mobi.zhangsheng.jiejia.domain.Areas;
import mobi.zhangsheng.jiejia.service.AgentsService;
import mobi.zhangsheng.jiejia.service.AreasService;
@Controller
public class ProvinceAction {
private int provinceid;
private int cityid;
@Resource
private AreasService as;
@Resource
private AgentsService ags;
public int getProvinceid() {
return provinceid;
}
public void setProvinceid(int provinceid) {
this.provinceid = provinceid;
}
public int getCityid() {
return cityid;
}
public void setCityid(int cityid) {
this.cityid = cityid;
}
public void getProvince(){
List provinceList = as.getAreasPrvinceList();
HttpServletResponse resp= ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
//resp.setContentType("xml");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
try {
PrintWriter out = resp.getWriter();
out.print("");
//out.print("shanghai");
} catch (IOException e) {
e.printStackTrace();
}
}
public void getCity(){
List cityList = as.getAreasCityList(provinceid);
HttpServletResponse resp= ServletActionContext.getResponse();
//resp.setContentType("xml");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
try {
PrintWriter out = resp.getWriter();
out.print("");
//out.print("shanghai");
} catch (IOException e) {
e.printStackTrace();
}
}
public void getArea(){
List areaList = as.getAreasCityList(cityid);
if(areaList.size()==0){
}else{
HttpServletResponse resp= ServletActionContext.getResponse();
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
try {
PrintWriter out = resp.getWriter();
out.print("");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}