问题请教

zhbw 2004-09-30 10:09:56
请问如何实现第二个下拉框的内容随第一个下拉框的内容变化而变化的
...全文
64 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhbw 2004-10-01
  • 打赏
  • 举报
回复
多谢,多谢,谢
风斧 2004-10-01
  • 打赏
  • 举报
回复
我觉得楼上的效果可以用javascript来实现,如果涉及到后台数据的刷新,applet就不行了吧。可以有一个提交的操作来解决。
lanleer 2004-09-30
  • 打赏
  • 举报
回复
我来个简单得例子

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JComboBoxSample extends JFrame implements ItemListener
{
Container ctp=getContentPane();

JLabel lb1=new JLabel("类型:");

String obj[]={"铅笔","钢笔"},
type1[]={"A","B","C"},
type2[]={"E","F","G"};

JComboBox cbx=new JComboBox();
JComboBox cbx2=new JComboBox();

public JComboBoxSample()
{
ctp.setLayout(new FlowLayout());
for (int n=0;n<obj.length;n++)
cbx.addItem(obj[n]);
ctp.add(cbx);
cbx.addItemListener(this);
ctp.add(lb1);
ctp.add(cbx2);
for (int n=0;n<type1.length;n++)
cbx2.addItem(type1[n]);

}
public void itemStateChanged(ItemEvent e)

{

int x=0,y;

String ko=(String)e.getItem();
for (y=0;y<type1.length;y++)

x=cbx.getSelectedIndex();
System.out.println(x);
if (x==1)
{
cbx2.removeAllItems();
for (int n=0;n<type2.length;n++)
cbx2.addItem(type2[n]);

}
if (x==0)
{
cbx2.removeAllItems();
for (int n=0;n<type1.length;n++)
cbx2.addItem(type1[n]);

}

}
public static void main(String []args)
{
JComboBoxSample jcb=new JComboBoxSample();
jcb.setSize(310,150);
jcb.setVisible(true);
}
}
hbuzhang 2004-09-30
  • 打赏
  • 举报
回复
给你个例子你研究一下:
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<script language="javascript">
cityareaname=new Array(35);
cityareacode=new Array(35);
function first(preP,preC,formname,selectP,selectC)
{
a=0;
if (selectP=='01')
{ a=1;tempoption=new Option('北京','01',false,true); }
else
{ tempoption=new Option('北京','01'); }
eval('document.'+formname+'.'+preP+'.options[1]=tempoption;');
cityareacode[0]=new Array('0101','0102','0103','0104','0105','0106','0107','0108');
cityareaname[0]=new Array('东城区','西城区','崇文区','宣武区','朝阳区','海淀区','丰台区','石景山');
if (selectP=='02')
{ a=2;tempoption=new Option('深圳','02',false,true); }
else
{ tempoption=new Option('深圳','02'); }
eval('document.'+formname+'.'+preP+'.options[2]=tempoption;');
cityareacode[1]=new Array('0201','0202','0203','0204','0205','0206');
cityareaname[1]=new Array('罗湖','福田','南山','盐田','宝安','龙岗');
if (selectP=='03')
{ a=3;tempoption=new Option('上海','03',false,true); }
else
{ tempoption=new Option('上海','03'); }
eval('document.'+formname+'.'+preP+'.options[3]=tempoption;');
cityareacode[2]=new Array('0301','0302','0303','0304','0305','0306','0307','0308','0309','0310','0311','0312','0313','0314','0315','0316','0317','0318','0319','0320');
cityareaname[2]=new Array('宝山','金山','南市','长宁','静安','青浦','崇明','卢湾','松江','奉贤','浦东','杨浦','虹口','普陀','闸北','黄浦','闵行','徐汇','嘉定','南汇');

eval('document.'+formname+'.'+preP+'.options[33]=tempoption;');
cityareacode[32]=new Array();
cityareaname[32]=new Array();
if (selectP=='34')
{ a=34;tempoption=new Option('澳门','34',false,true); }
else
{ tempoption=new Option('澳门','34'); }
eval('document.'+formname+'.'+preP+'.options[34]=tempoption;');
cityareacode[33]=new Array();
cityareaname[33]=new Array();
if (selectP=='35')
{ a=35;tempoption=new Option('台湾','35',false,true); }
else
{ tempoption=new Option('台湾','35'); }
eval('document.'+formname+'.'+preP+'.options[35]=tempoption;');
cityareacode[34]=new Array();
cityareaname[34]=new Array();

eval('document.'+formname+'.'+preP+'.options[a].selected=true;');

cityid=selectP;
if (cityid!='0')
{
b=0;for (i=0;i<cityareaname[cityid-1].length;i++)
{
if (selectC==cityareacode[cityid-1][i])
{b=i+1;tempoption=new Option(cityareaname[cityid-1][i],cityareacode[cityid-1][i],false,true);}
else
tempoption=new Option(cityareaname[cityid-1][i],cityareacode[cityid-1][i]);
eval('document.'+formname+'.'+preC+'.options[i+1]=tempoption;');
}
eval('document.'+formname+'.'+preC+'.options[b].selected=true;');
}
}
function selectcityarea(preP,preC,formname)
{
cityid=eval('document.'+formname+'.'+preP+'.selectedIndex;');
j=eval('document.'+formname+'.'+preC+'.length;');
for (i=1;i<j;i++)
{eval('document.'+formname+'.'+preC+'.options[j-i]=null;')}
if (cityid!="0")
{
for (i=0;i<cityareaname[cityid-1].length;i++)
{
tempoption=new Option(cityareaname[cityid-1][i],cityareacode[cityid-1][i]);
eval('document.'+formname+'.'+preC+'.options[i+1]=tempoption;');
}
}
}
</script>
<form action="file:///C|/Program%20Files/Macromedia/Dreamweaver%204/Lessons/Lesson%20Files/info_search.asp?martid=" method=post name=form1>
<img src="file:///C|/Program%20Files/Macromedia/Dreamweaver%204/images/w_KT.gif" height="6" width="1"><br>
<table width=300 border=0 cellspacing="0" cellpadding="1" !bgcolor=#FFFDE6>
<tr>
<td class="black_con9" align="right">省份:</td>
<td class="black_con9">
<select name="selectp" onChange="selectcityarea('selectp','selectc','form1');">
<option value="0" selected>省份</option>
</select>
</td>
</tr>
<tr>
<td class="black_con9" align="right">县/市:</td>
<td class="black_con9">
<select name="selectc">
<option value="0" selected>县/市</option>
</select>
</td>
</tr>
</table>
</form>
<script language="javascript">
first("selectp","selectc","form1",0,0);
</script>


</body>
</html>

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧