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" onclick="check(this.value)"/>
Internet Explorer<br/>
<input type="radio" name="browser" value="Firefox" onclick="check(this.value)"/>
Firefox<br/>
<input type="radio" name="browser" value="Netscape" onclick="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>
<