系统单据的一个分类需要禁用(不是删除),做单据的表单是用的下拉列表,马上就想到了 把那个option disabled.
1: function diableSelectItem(id,v){
2: var el = document.getElementById(id);
3: for(var i=0,j = el.options.length;i<j;i++){
4: if(el.options[i].value == v){ el.options[i].disabled=true;break;}
5: }
6: }
OK,运行,发现选项没有显示灰色,仍然可用。!!!代码没问题啊,怎么办? 突发奇想 拿个IE8来试试,竟然禁用了。NND 是IE的问题
怎么办,有问题,找google 发现 Select, Option, Disabled And The JavaScript Solution
有老外已经搞定了。贴代码:
1: /****************************************************************
2: * Author: Alistair Lattimore
3: * Website: http://www.lattimore.id.au/
4: * Contact: http://www.lattimore.id.au/contact/
5: * Errors, suggestions or comments
6: * Date: 30 June 2005
7: * Version: 1.0
8: * Purpose: Emulate the disabled attributte for the <option>
9: * element in Internet Explorer.
10: * Use: You are free to use this script in non-commercial
11: * applications. You are however required to leave
12: * this comment at the top of this file.
13: *
14: * I'd love an email if you find a use for it on your
15: * site, though not required.
16: ****************************************************************/
17:
18: window.onload = function() {
19: if (document.getElementsByTagName) {
20: var s = document.getElementsByTagName("select");
21:
22: if (s.length > 0) {
23: window.select_current = new Array();
24:
25: for (var i=0, select; select = s[i]; i++) {
26: select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
27: select.onchange = function(){ restore(this); }
28: emulate(select);
29: }
30: }
31: }
32: }
33:
34: function restore(e) {
35: if (e.options[e.selectedIndex].disabled) {
36: e.selectedIndex = window.select_current[e.id];
37: }
38: }
39:
40: function emulate(e) {
41: for (var i=0, option; option = e.options[i]; i++) {
42: if (option.disabled) {
43: option.style.color = "graytext";
44: }
45: else {
46: option.style.color = "menutext";
47: }
48: }
49: }
模拟了disabled的效果,改变文本颜色,当一个选项有 disabled属性,就让select元素选择索引回滚默认值
另附上判断IE版本的js代码 来自 司徒正美
1: var ie = (function() {
2: var v = 3, div = document.createElement('div'), a = div.all || [];
3: while (div.innerHTML = '<!--[if gt IE '+(++v)+']><br><![endif]-->', a[0]);
4: return v > 4 ? v : !v;
5: }());