实现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 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>
详细解决方案
js使select标签option替不可选
热度:130 发布时间:2012-11-01 11:11:32.0
相关解决方案
- Struts2 <select>上拉框 回显有关问题
- 關於Struts2 select tag 問題,该怎么处理
- <xsl:for-each select= 取嵌套节点则么写,该如何解决
- <select>标签可平添文字
- Struts2 <s:select/>有关问题
- jsp页面<select>选中有关问题
- struts2 中用两个<s:select>标签如何实现级联的效果
- <s:select>标签,小弟我要通过js获取listKey的值要如何获取
- <s:select>解决办法
- 关于strust2 <s:select>标签组值有关问题
- select count(*)as num from hall where Hall_No=10000001关于这个SQL语句,怎么取出返回的数字
- js里如何取<s:select>标签里的值
- Eclipse导入工程后,XDoclet异常:Missing library: xdoclet-1.2.1.jar. Select the home direc
- magaView.HasMonth = db.Library.GroupBy(a => a.TimeBook.Month).Select(a => a.Key),该怎么处理
- string sql = @"select."该如何处理
- 经过后台代码为html控件<select>绑定数据?求大神指点!
- 标签<select>事件,该如何处理
- SELECT RUNAT="SERVER" ONCHANGE,该怎么处理
- .net怎么用ajax,js 为标签<select>动态添加数据
- sqltxet能不能这么写成"select *rowid from table"
- Select @NewID,该怎么解决
- select * from BigClass where FatherID=? order by Order desc解决思路
- 错误详细信息: System.Data.OleDb.OleDbException: SELECT 子句中包含一个保留字、拼写异常或丢失的参数,或标点符号不正确
- select 约束中怎么将text所填写的内容作为约束条件
- select SCOPE_IDENTITY失效
- 页面上有个<select></select>用js或jq或后台写个循环生成一个连续的年份如2000-2012然后添加到select下拉框,value值对应年份解决方案
- select * from lesson where username='name' order by id desc解决思路
- 请教dt.select()中的条件可以是大小判断吗
- 语法异常。 在查询表达式 'ID not in (select top 12*1 ID from prod_news order by infotime des
- .NET MVC3 from mvc3 select,该怎么解决