js动态操作select下拉列表(详细代码)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function check(browser){
document.getElementById("answer").value=browser;
}
function cut()
{
var s =document.getElementById("s").length;
var valueOne=document.getElementById("s").value;
var optionLength = document.getElementById("d").length;
if (optionLength == 0) {
document.getElementById("d").options.add(new Option(document
.getElementById("s").options[0].value, 0));
document.getElementById("s").options.remove(0);
}
if (optionLength == 1) {
document.getElementById("d").options.add(new Option(document
.getElementById("s").options[0].value, 0));
document.getElementById("s").options.remove(0);
}
if (optionLength == 2) {
document.getElementById("d").options.add(new Option(document
.getElementById("s").options[0].value, 0));

document.getElementById("s").options.remove(0);
}
if (optionLength == 3) {
document.getElementById("d").options.add(new Option(document
.getElementById("s").options[0].value, 0));
document.getElementById("s").options.remove(0);
document.getElementById("button1").disabled="true";
}
}
function copy() {
//得到select的长度
var optionLength = document.getElementById("d").length;
if (optionLength == 0) {
//动态添加option
document.getElementById("d").options.add(new Option(document
.getElementById("s").options[0].value, 0));
}
if (optionLength == 1) {
document.getElementById("d").options.add(new Option(document
.getElementById("s").options[1].value, 1));
}
if (optionLength == 2) {
document.getElementById("d").options.add(new Option(document
.getElementById("s").options[2].value, 2));
}
if (optionLength == 3) {
document.getElementById("d").options.add(new Option(document
.getElementById("s").options[3].value, 3));
document.getElementById("button2").disabled="true";
}
}
</script>
</head>
<body>
<p>您喜欢哪款浏览器?点击单选按钮,然后把值赋到文本框里面</p>
<input type="radio" name="browser" value="Internet Explorer" οnclick="check(this.value)"/>
Internet Explorer<br/>
<input type="radio" name="browser" value="Firefox" οnclick="check(this.value)"/>
Firefox<br/>
<input type="radio" name="browser" value="Netscape" οnclick="check(this.value)"/>
Netscape<br/>
你喜欢的浏览器是:
<input type="text" id="answer" size="20"/>
<form>
<p>@1select列表</p>
<select id="s">
<option value="苹果">苹果</option>
<option value="香蕉">香蕉</option>
<option value="菠萝">菠萝</option>
<option value="橘子">橘子</option>
</select>
<p>@2select列表</p>
<select id="d">

</select>
<input type="button" οnclick="cut()" value="剪切" id="button1"/>
<input type="button" id="button2" οnclick="copy()" value="把select1里面的数据复制到select2里面" />
</form>
</body>

</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值