怎么让select无法选中option
2016-12-09
展开全部
<!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>
<script>
function aa(a){
if(a.value==3){
a.options[2].removeAttribute("selected");
a.options[0].setAttribute("selected","selected");
alert("不能选择c");
}
}
</script>
<body>
<select onchange="aa(this)">
<option value="1">a</option>
<option value="2">b</option>
<option value="3" disabled="disabled">c</option>
</select>
楼上回答的只适应IE8、火狐;IE8以下不管用。
结合楼上的这样写!
</body>
</html>
或者把这段js加到网页中,其它的什么也不用管
window.onload = function() {
if (document.getElementsByTagName) {
var s = document.getElementsByTagName("select");
if (s.length > 0) {
window.select_current = new Array();
for (var i=0, select; select = s[i]; i++) {
select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
select.onchange = function(){ restore(this); }
emulate(select);
}
}
}
}
function restore(e) {
if (e.options[e.selectedIndex].disabled) {
e.selectedIndex = window.select_current[e.id];
}
}
function emulate(e) {
for (var i=0, option; option = e.options[i]; i++) {
if (option.disabled) {
option.style.color = "graytext";
}
else {
option.style.color = "menutext";
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script>
function aa(a){
if(a.value==3){
a.options[2].removeAttribute("selected");
a.options[0].setAttribute("selected","selected");
alert("不能选择c");
}
}
</script>
<body>
<select onchange="aa(this)">
<option value="1">a</option>
<option value="2">b</option>
<option value="3" disabled="disabled">c</option>
</select>
楼上回答的只适应IE8、火狐;IE8以下不管用。
结合楼上的这样写!
</body>
</html>
或者把这段js加到网页中,其它的什么也不用管
window.onload = function() {
if (document.getElementsByTagName) {
var s = document.getElementsByTagName("select");
if (s.length > 0) {
window.select_current = new Array();
for (var i=0, select; select = s[i]; i++) {
select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
select.onchange = function(){ restore(this); }
emulate(select);
}
}
}
}
function restore(e) {
if (e.options[e.selectedIndex].disabled) {
e.selectedIndex = window.select_current[e.id];
}
}
function emulate(e) {
for (var i=0, option; option = e.options[i]; i++) {
if (option.disabled) {
option.style.color = "graytext";
}
else {
option.style.color = "menutext";
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询