日期:2014-05-16 浏览次数:20573 次
实现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=" "? ></optgroup>
<option> 北京奔驰—戴克 </option>
<option> 北京现代 </option>
<optgroup label=" " ></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 c