当前位置: 代码迷 >> JavaScript >> js使select标签option替不可选
  详细解决方案

js使select标签option替不可选

热度:130   发布时间:2012-11-01 11:11:32.0
js使select标签option为不可选

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

1. 使用optgroup标签及其label属性.兼容于IE,FF.例子如下:
?? <style type="text/css" media="all">
option.readOnly {background:#ccc;color:#666;}
</style>
<select onchange="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>

  相关解决方案