如何使用js 控制select 不能选择重复的值
展开全部
可以使用javascript和jQuery两种实现方式
1:使用javascript实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<select name="jumpMenu" id="jumpMenu" onChange="jumpMenu('parent',this,0)">
<option id="1" value="跳转URL">111</option> // 111 是显示给用户的信息
<option id="2" value="跳转URL">222</option>
<option id="3" value="跳转URL">333</option>
<option id="4" value="跳转URL">444</option>
<option id="5" value="跳转URL">555</option>
</select>
<script type="text/javascript">
function display(optionID){
var all_options = document.getElementById("jumpMenu").options;
for (i=0; i<all_options.length; i++){
if (all_options[i].id == optionID) // 根据option标签的ID来进行判断 测试的代码这里是两个等号
{
all_options[i].selected = true;
}
}
};
display("4");
</script>
</body>
</html>
2:使用jQuery实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<select name="jumpMenu" id="jumpMenu" >
<option value="1">111</option> // 111 是显示给用户的信息
<option value="2">222</option>
<option value="3">333</option>
<option value="4">444</option>
<option value="5">555</option>
</select>
<script type="text/javascript" src="js/jquery1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
// $("#jumpMenu").val(要选中的option的value值即可);
$("#jumpMenu").val(4);
});
</script>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script type="text/javascript">
<!--
window.onload = function(){
document.getElementsByTagName('button')[0].onclick = function(){
// 不能有相同取值的下拉列表集合
var items = document.getElementsByTagName('select'),
len = items.length,
i = 0,
valMap = {},
sel,
val;
// 遍历并保存各个下拉列表的值于valMap
for(; i < len; i++){
sel = items[i];
val = sel.options[sel.selectedIndex].value;
// 没有相同的取值时保存该值,否则返回false
if(!valMap[val]){
valMap[val] = 1;
}else{
alert('存在取值相同的下拉列表,取值为:' + val);
return false;
}
}
// 校验通过,全部都不相同
return true;
}
}
//-->
</script>
<body>
<button>Check</button>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
</body>
</html>
1:使用javascript实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<select name="jumpMenu" id="jumpMenu" onChange="jumpMenu('parent',this,0)">
<option id="1" value="跳转URL">111</option> // 111 是显示给用户的信息
<option id="2" value="跳转URL">222</option>
<option id="3" value="跳转URL">333</option>
<option id="4" value="跳转URL">444</option>
<option id="5" value="跳转URL">555</option>
</select>
<script type="text/javascript">
function display(optionID){
var all_options = document.getElementById("jumpMenu").options;
for (i=0; i<all_options.length; i++){
if (all_options[i].id == optionID) // 根据option标签的ID来进行判断 测试的代码这里是两个等号
{
all_options[i].selected = true;
}
}
};
display("4");
</script>
</body>
</html>
2:使用jQuery实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<select name="jumpMenu" id="jumpMenu" >
<option value="1">111</option> // 111 是显示给用户的信息
<option value="2">222</option>
<option value="3">333</option>
<option value="4">444</option>
<option value="5">555</option>
</select>
<script type="text/javascript" src="js/jquery1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
// $("#jumpMenu").val(要选中的option的value值即可);
$("#jumpMenu").val(4);
});
</script>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script type="text/javascript">
<!--
window.onload = function(){
document.getElementsByTagName('button')[0].onclick = function(){
// 不能有相同取值的下拉列表集合
var items = document.getElementsByTagName('select'),
len = items.length,
i = 0,
valMap = {},
sel,
val;
// 遍历并保存各个下拉列表的值于valMap
for(; i < len; i++){
sel = items[i];
val = sel.options[sel.selectedIndex].value;
// 没有相同的取值时保存该值,否则返回false
if(!valMap[val]){
valMap[val] = 1;
}else{
alert('存在取值相同的下拉列表,取值为:' + val);
return false;
}
}
// 校验通过,全部都不相同
return true;
}
}
//-->
</script>
<body>
<button>Check</button>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
</body>
</html>
2017-06-27
展开全部
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script type="text/javascript">
<!--
window.onload = function(){
document.getElementsByTagName('button')[0].onclick = function(){
// 不能有相同取值的下拉列表集合
var items = document.getElementsByTagName('select'),
len = items.length,
i = 0,
valMap = {},
sel,
val;
// 遍历并保存各个下拉列表的值于valMap
for(; i < len; i++){
sel = items[i];
val = sel.options[sel.selectedIndex].value;
// 没有相同的取值时保存该值,否则返回false
if(!valMap[val]){
valMap[val] = 1;
}else{
alert('存在取值相同的下拉列表,取值为:' + val);
return false;
}
}
// 校验通过,全部都不相同
return true;
}
}
//-->
</script>
<body>
<button>Check</button>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
</body>
</html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script type="text/javascript">
<!--
window.onload = function(){
document.getElementsByTagName('button')[0].onclick = function(){
// 不能有相同取值的下拉列表集合
var items = document.getElementsByTagName('select'),
len = items.length,
i = 0,
valMap = {},
sel,
val;
// 遍历并保存各个下拉列表的值于valMap
for(; i < len; i++){
sel = items[i];
val = sel.options[sel.selectedIndex].value;
// 没有相同的取值时保存该值,否则返回false
if(!valMap[val]){
valMap[val] = 1;
}else{
alert('存在取值相同的下拉列表,取值为:' + val);
return false;
}
}
// 校验通过,全部都不相同
return true;
}
}
//-->
</script>
<body>
<button>Check</button>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
<select>
<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>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询