1 $(function () { 2 SetControlDisabled(0, "", true); //控制grid的第一行的所有控件不可用 3 SetControlDisabled("", 1, true); //控制grid的第二列的所有控件不可用 4 SetControlDisabled("", "标题3", true); //控制grid的标题是‘标题3’的那一列的所有控件不可用 5 SetControlDisabled(2, "标题5", true); //控制grid的第三行并且标题是‘标题5’的那一列的所有控件不可用 6 //SetControlDisabled(true);控制grid里面的所有控件不可用 7 8 var controls = findControl(1);//查找第二行的所有控件 9 for (var control in controls) { 10 alert(controls[control].attr("id")); 11 } 12 13 var targetControls = findControl(0, 1); //查找第一行,第2列的所有控件 14 for (var control in targetControls) { 15 alert(targetControls[control].attr("id")); 16 } 17 18 String.prototype.trim = function () { 19 return this.replace(/^(\s|\u3000)+|(\s|\u3000)+$/g, ""); 20 } 21 22 //根据表头标题找到表头所在的列索引 23 function GetTdIndexByTdText(strTdText) { 24 var targetTdIndex = 0; 25 $("#grid thead tr").find("th").each(function (index, item) { 26 if ($(item).text().trim() == strTdText) { 27 targetTdIndex = index; 28 return false; 29 } 30 }); 31 return targetTdIndex; 32 } 33 //查找grid里面的控件 34 function findControl() { 35 var controls = []; 36 var controlId = null; 37 if (arguments.length == 1) { 38 var targetTr = $("#grid tbody tr").eq(arguments[0]); 39 targetTr.find("td").each(function (index, item) { 40 $(item).find("input").each(function (i_index, i_item) { 41 controlId = $(i_item).attr("id") 42 if (controlId === "") { 43 controlId = newGuid(); 44 $(i_item).attr("id", controlId); 45 } 46 controls[controlId] = $(i_item); 47 }); 48 $(item).find("select").each(function (i_index, i_item) { 49 controlId = $(i_item).attr("id") 50 if (controlId === "") { 51 controlId = newGuid(); 52 $(i_item).attr("id", controlId); 53 } 54 controls[controlId] = $(i_item); 55 }); 56 $(item).find("textarea").each(function (i_index, i_item) { 57 controlId = $(i_item).attr("id") 58 if (controlId === "") { 59 controlId = newGuid(); 60 $(i_item).attr("id", controlId); 61 } 62 controls[controlId] = $(i_item); 63 }); 64 }); 65 } else if (arguments.length == 2) { 66 var targetTr = $("#grid tbody tr").eq(arguments[0]); 67 var targetTd = targetTr.find("td").eq(arguments[1]); 68 targetTd.find("input").each(function (index, item) { 69 controlId = $(item).attr("id") 70 if (controlId === "") { 71 controlId = newGuid(); 72 $(item).attr("id", controlId); 73 } 74 controls[controlId] = $(item); 75 76 }); 77 targetTd.find("select").each(function (index, item) { 78 controlId = $(item).attr("id") 79 if (controlId === "") { 80 controlId = newGuid(); 81 $(item).attr("id", controlId); 82 } 83 controls[controlId] = $(item); 84 85 }); 86 targetTd.find("textarea").each(function (index, item) { 87 controlId = $(item).attr("id") 88 if (controlId === "") { 89 controlId = newGuid(); 90 $(item).attr("id", controlId); 91 } 92 controls[controlId] = $(item); 93 94 }); 95 } 96 return controls; 97 } 98 99 //控制grid里面的控件是否可用100 function SetControlDisabled(rowIndex, colIndex, bolFlag) {101 //SetGridControlEnabeld(rowIndex,"",bolFlag) 102 if (rowIndex >= 0 && colIndex == "") {103 var targetTr = $("#grid tbody tr").eq(rowIndex);104 targetTr.find("td").each(function (index, item) {105 SetDisabled(item, bolFlag);106 });107 }108 //SetGridControlEnabeld("",colIndex,bolFlag) 109 else if (rowIndex == "") {110 $("#grid tbody tr").each(function (index, item) {111 var td = null;112 if (typeof (colIndex) == "string") {113 td = $(item).find("td").eq(GetTdIndexByTdText(colIndex))114 } else {115 td = $(item).find("td").eq(colIndex)116 }117 118 SetDisabled(td, bolFlag);119 });120 } else if (rowIndex >= 0 && typeof (rowIndex) == "number") {121 var targetTr = $("#grid tbody tr").eq(rowIndex);122 var targetTd = null;123 if (typeof (colIndex) == "string") {124 targetTd = targetTr.find("td").eq(GetTdIndexByTdText(colIndex))125 } else {126 targetTd = targetTr.find("td").eq(colIndex)127 }128 SetDisabled(targetTd, bolFlag);129 130 } else if (arguments.length == 1) {131 var bolFlag = arguments[0];132 $("#grid tbody tr").each(function (trIndex, trItem) {133 $(trItem).find("td").each(function (tdIndex, tdItem) {134 SetDisabled(tdItem, bolFlag);135 });136 });137 }138 }139 140 //生成 GUID 141 function newGuid() {142 var guid = "";143 for (var i = 1; i <= 32; i++) {144 var n = Math.floor(Math.random() * 16.0).toString(16);145 guid += n;146 if ((i == 8) || (i == 12) || (i == 16) || (i == 20))147 guid += "-";148 }149 return guid;150 }151 152 //设置某个容器里面的控件是否可用153 function SetDisabled(container, bolFlag) {154 $(container).find("input").each(function (index, item) {155 $(item).attr('disabled', bolFlag);156 });157 $(container).find("select").each(function (index, item) {158 $(item).attr('disabled', bolFlag);159 });160 $(container).find("textarea").each(function (index, item) {161 $(item).attr('disabled', bolFlag);162 });163 }164 });
HTML测试代码:
123 4
945 24 25 266 标题1 7 89 标题210 1112 标题313 1415 标题416 1718 标题519 2021 标题622 2327 4828 29 3031 34 3536 37 3839 40 4142 43 4445 46 4749 7050 51 5253 56 5758 59 6061 62 6364 65 6667 68 6971 92 9372 73 7475 78 7980 81 8283 84 8586 87 8889 90 91