论坛首页 综合技术论坛

IE8/IE7/IE6下Select列表中disabled的使用

浏览 7012 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-04   最后修改:2009-03-25
昨天项目中使用select列表list-to-list,其中右边有一项是不可以让用户选中,就是必须选,我使用了selectedFields.options[0].disabled = 'disabled';或者selectedFields.options[0].disabled = true;在IE8下面是正常的,不可选的,但在IE6和IE7下没有起到作用,试了几种方法都不行,最后在网上找到下面代码,只要copy到你的js文件里,然后使用上面的disabled就可以做到不可选的效果了。
有用到可以看看:
/****************************************************************
* 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++) {
if(select.id == "your select id") {[//你的select id
select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
				select.onchange = function(){ restore(this); }
				emulate(select);
			[color=red]}[/color]}
		}
	}
}

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";
		}
	}
}

   发表时间:2009-09-23  
如果在select 中添加onchange="" 将会无效,请问怎么解决啊·~?如下代码
<select id="s1" onchange="alert(this.selectedIndex);">
  <option value="p1" >p1</option>
  <option value="p2" >P0</option>
  <option value="p3" >P3</option>
  <option value="p4" >P4</option>
</option>
onchange方法被select-option-disabled-emulation.js代码中select.onchange = function(){ restore(this); } 覆盖
0 请登录后投票
   发表时间:2009-09-23  
f0799 写道
如果在select 中添加onchange="" 将会无效,请问怎么解决啊·~?如下代码
<select id="s1" onchange="alert(this.selectedIndex);">
  <option value="p1" >p1</option>
  <option value="p2" >P0</option>
  <option value="p3" >P3</option>
  <option value="p4" >P4</option>
</option>
onchange方法被select-option-disabled-emulation.js代码中select.onchange = function(){ restore(this); } 覆盖

你要使用onchange事件是话,可能只有把select-option-disabled-emulation.js代码换成其它事件类似替代它。可以试试看先...
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics