select标签的相关操作,选中,获取option的值,二级联动

时间:2022-08-17 06:03:57

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="./js/jquery-1.11.2.min.js"></script>
<script>
function selectAllOptionVal() {

var str = "";
$("#select-option-one option").each(function(i) {

var v = $(this).val();
str = str + "\n" + v;
});

alert(str);
}

function selectedOptionVal() {

var s = $("#select-option-one").val();
alert(s);
}

function noSelectedOptionVal() {

var str = '';
$("#select-option-one option").each(function(i) {

if (!$(this).is(":selected")) {

str = str + "\n" + $(this).val();
}

});
alert(str);

}

function oneToTwo() {

v = $("#select-option-one").val();

/*
//获取被选中的参数作为值可以异步获取数据
$.ajax({

});
*/

//为第二级加载数据
//先清空上一次加载的内容
$("[name='select-option-two-option']").remove();
//以下是例子的数据
var arr = new Array(5, 6, 7, 8, 9, 10);

//拼接的html
var str = "";
for (var i = 0; i < arr.length; i++) {

str = str
+ "<option name='select-option-two-option' value='"+arr[i]+"'>"
+ arr[i] + "</option>";

}

//为第二级select添加内容
$("#select-option-two").append(str);
}
</script>
</head>
<body>

<table>

<tr>
<td><select id="select-option-one" onchange="oneToTwo();">
<option value='1'>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value='5'>5</option>
</select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <select id="select-option-two">

</select></td>
<td>
<button onclick="selectAllOptionVal();">获取所有option的值</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<button onclick="selectedOptionVal();">获取选中option的值</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<button onclick="noSelectedOptionVal();">获取所有未被选中的值</button>
</td>
</tr>

</table>

</body>
</html>