JS实现两个下拉框的选择问题
两个下拉框A和B,A里面的选项为1,2,3;B里面的选项为4,5,6;当我选择在A下拉框里选择1时,B框里选项为4,5,6;选择2时,B框里选项为5,6;选择3时,B框里...
两个下拉框A和B,A里面的选项为1,2,3;B里面的选项为4,5,6;
当我选择在A下拉框里选择1时,B框里选项为4,5,6;选择2时,B框里选项为5,6;选择3时,B框里选项为6;
这个怎么用JS实现?我要代码!谢谢! 展开
当我选择在A下拉框里选择1时,B框里选项为4,5,6;选择2时,B框里选项为5,6;选择3时,B框里选项为6;
这个怎么用JS实现?我要代码!谢谢! 展开
2个回答
展开全部
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<script type="text/javascript">
function change(){
document.getElementById('B').innerText = '';
var tem = document.getElementById('A').value;
if(tem == 1){
document.getElementById('B').options[0] = new Option('4','4');
document.getElementById('B').options[1] = new Option('5','5');
document.getElementById('B').options[2] = new Option('6','6');
}else if(tem == 2){
document.getElementById('B').options[0] = new Option('5','5');
document.getElementById('B').options[1] = new Option('6','6');
}else if(tem == 3){
document.getElementById('B').options[0] = new Option('6','6');
}
}
function test(){
change();
}
</script>
<body onload="test()">
<select id="A" onchange="change()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br>
<select id="B">
</select>
</body>
</html>
把上面的东西保存成htm文件就OK了,看看是不是你要的
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<script type="text/javascript">
function change(){
document.getElementById('B').innerText = '';
var tem = document.getElementById('A').value;
if(tem == 1){
document.getElementById('B').options[0] = new Option('4','4');
document.getElementById('B').options[1] = new Option('5','5');
document.getElementById('B').options[2] = new Option('6','6');
}else if(tem == 2){
document.getElementById('B').options[0] = new Option('5','5');
document.getElementById('B').options[1] = new Option('6','6');
}else if(tem == 3){
document.getElementById('B').options[0] = new Option('6','6');
}
}
function test(){
change();
}
</script>
<body onload="test()">
<select id="A" onchange="change()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br>
<select id="B">
</select>
</body>
</html>
把上面的东西保存成htm文件就OK了,看看是不是你要的
展开全部
<html>
<head>
<title>js</title>
<script type="text/javascript" language="javascript">
function clickHandler() {
var opt2 = document.getElementById("select2");
for (var i = opt2.options.length - 1; i >= 0; i--) {
opt2.options.remove(i);
}
var opt1 = document.getElementById("select1");
if(opt1.options[0].selected) {
opt2.add(createOption("4", "4"));
opt2.add(createOption("5", "5"));
opt2.add(createOption("6", "6"));
} else if(opt1.options[1].selected) {
opt2.add(createOption("5", "5"));
opt2.add(createOption("6", "6"));
} else if(opt1.options[2].selected) {
opt2.add(createOption("6", "6"));
}
}
function createOption(value, text) {
var opt = document.createElement("OPTION");
opt.value = value;
opt.text = text;
return opt;
}
</script>
</head>
<body>
<select name="select1" onchange="clickHandler()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="select2" onchange="clickHandler()">
<option value="1">4</option>
<option value="2">5</option>
<option value="3">6</option>
</select>
</body>
</html>
<head>
<title>js</title>
<script type="text/javascript" language="javascript">
function clickHandler() {
var opt2 = document.getElementById("select2");
for (var i = opt2.options.length - 1; i >= 0; i--) {
opt2.options.remove(i);
}
var opt1 = document.getElementById("select1");
if(opt1.options[0].selected) {
opt2.add(createOption("4", "4"));
opt2.add(createOption("5", "5"));
opt2.add(createOption("6", "6"));
} else if(opt1.options[1].selected) {
opt2.add(createOption("5", "5"));
opt2.add(createOption("6", "6"));
} else if(opt1.options[2].selected) {
opt2.add(createOption("6", "6"));
}
}
function createOption(value, text) {
var opt = document.createElement("OPTION");
opt.value = value;
opt.text = text;
return opt;
}
</script>
</head>
<body>
<select name="select1" onchange="clickHandler()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="select2" onchange="clickHandler()">
<option value="1">4</option>
<option value="2">5</option>
<option value="3">6</option>
</select>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询