js使select标签option为不可选

实现select标签的部分option为不可选有两种方法:

1. 使用optgroup标签及其label属性.兼容于IE,FF.例子如下:
   <style type="text/css" media="all">
option.readOnly {background:#ccc;color:#666;}
</style>
<select οnchange="if(this.options[selectedIndex].className=='readOnly') this.selectedIndex = -1">
<option value="===" class="readOnly">只读的 </option>
<option value="xxx" selected>可选的 </option> okjjnipujo
<option value="xxx">可选的 </option>
<option value="===" class="readOnly">只读的 </option>
<option value="xxx">可选的 </option>
<option value="xxx">可选的 </option>
<option value="===" class="readOnly">只读的 </option>
<option value="xxx">可选的 </option>
<option value="===" class="readOnly">只读的 </option>
<option value="xxx">可选的 </option>
<option value="xxx">可选的 </option>
<option value="===" class="readOnly">只读的 </option>
<option value="xxx">可选的 </option>
</select>
<select  name= "test ">
<optgroup  label="&nbsp;"  ></optgroup>
<option> 北京奔驰—戴克 </option>
<option> 北京现代 </option>
<optgroup label="&nbsp;" ></optgroup>
<option > 长安汽车 </option>
<option selected="selected"> 长安铃木 </option>
</select>

2.使用js进行监听方法.选择了不可选option时将它替换为原来的那个option.例子如下:
  /****************************************************************
* Author:    Alistair Lattimore
* Website:    http://www.lattimore.id.au/
* Contact:    http://www.lattimore.id.au/contact/
*            Errors, suggestions or comments
* Date:        30 June 2005
* Version:    1.0
* Purpose:    Emulate the disabled attributte for the <option>
*            element in Internet Explorer.
* Use:        You are free to use this script in non-commercial
*            applications. You are however required to leave
*            this comment at the top of this file.
*
*            I'd love an email if you find a use for it on your
*            site, though not required.
****************************************************************/

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; alert(" this.id= "+this.id+" this.selectedIndex= "+this.selectedIndex); }
                select.onchange = function(){ restore(this); }
                emulate(select);
                alert("i=  "+i+" select= "+select.value);
            }
        }
    }
}

function restore(e) {
    if (e.options[e.selectedIndex].disabled) {
        e.selectedIndex = window.select_current[e.id];
        alert(e.selectedIndex+" "+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";
        }
    }
}

===
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0089)http://www.lattimore.id.au/files/examples/form-select-option-disabled-attr-js-simple.html -->
<HTML><HEAD><TITLE>form select option disabled simple, using javascript emulation</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1"><!--[if lt IE 7]>
<SCRIPT
src="form select option disabled simple, using javascript emulation.files/select-option-disabled-emulation.js"
type=text/javascript></SCRIPT>
<![endif]-->
<SCRIPT
src="form select option disabled simple, using javascript emulation.files/select-option-disabled-emulation.js"
type=text/javascript></SCRIPT>

<META content="MSHTML 6.00.2900.3354" name=GENERATOR></HEAD>
<BODY>
<TABLE cellSpacing=5 cellPadding=3>
  <TBODY>
  <TR>
    <TD>
     <FORM action=# method=post>
      <SELECT id=dropdown_car name=dropdown_car>
        <OPTION value=ferrari selected>Ferrari</OPTION>
        <OPTION value=ford>Ford</OPTION>
        <OPTION disabled value=holden>Holden</OPTION>
        <OPTION disabled value=mitsubishi>Mitsubishi</OPTION>
        <OPTION value=porsche>Porsche</OPTION>
        <OPTION disabled value=toyota>Toyota</OPTION>
      </SELECT>
      <INPUT type=submit value=Submit>
      </FORM>
      </TD>
    <TD>
    
      <FORM action=# method=post>
        <SELECT id=dropdown_animal name=dropdown_animal>
          <OPTION value=cat selected>Cat</OPTION>
          <OPTION disabled value=snake>Snake</OPTION>
          <OPTION disabled value=spider>Spider</OPTION>
          <OPTION value=dog>Dog</OPTION>
        </SELECT>
      <INPUT type=submit value=Submit>
      </FORM>
      </TD></TR></TBODY></TABLE>
<P>This page has standard dropdown lists, which are emulated when the page is
initially rendered.</P></BODY></HTML>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值