0

我正在动态地将行添加到表中。每行都有输入字段以及下拉字段。当我在下拉列表中选择“其他”时,我想禁用“评论”字段。如何检查动态添加行的下拉框?

以下是我的代码。行插入工作正常,但我不知道如何检查下拉框的选定值。

<script language="javascript" type="text/javascript">
function addRow() {
   var tbl = document.getElementById('tblSample');
   var lastRow = tbl.rows.length;   
   var iteration = lastRow;
   var row = tbl.insertRow(lastRow);

   var cellLeft = row.insertCell(0);
   var textNode = document.createTextNode(iteration-3);
   cellLeft.appendChild(textNode);

   var cellRightSel = row.insertCell(1);
   var sel = document.createElement('select');
   sel.name = 'LOCsel' + iteration;
   sel.options[0] = new Option('---Any---', '0');
   sel.options[1] = new Option('Level 0.5: Early Intervention', '1');
   sel.options[2] = new Option('Level I: Outpatient', '2');
   sel.options[3] = new Option('Level I.D: Outpatient-Detox', '3');
   sel.options[4] = new Option('Level II.1: Intensive Outpatient', '4');
   sel.options[5] = new Option('Level II.5: Partial Hospitalization', '5');
   sel.options[6] = new Option('Level II.D: IOP-Detox', '6');
   sel.options[7] = new Option('Level III.1: Halfway House', '7');
   sel.options[8] = new Option('Level III.3: Long-Term Residential Care', '8');
   sel.options[9] = new Option('Level III.5: Therapeutic Community', '9');
   sel.options[10] = new Option('Level III.7: Medically Monitoried Inpatient(ICF)', '10');
   sel.options[11] = new Option('Level III.7.D: Med.Mon.Inpatient(ICF)-Detox', '11');
   sel.options[12] = new Option('Other', '12');
   cellRightSel.appendChild(sel);

   var cellRights = row.insertCell(2);
   var els = document.createElement('input');
   els.type = 'text';
   els.name = 'Comments' + iteration;
   els.id = 'Comments' + iteration;
   els.size = 30;
   cellRights.appendChild(els);

   var cell8 = row.insertCell(9);            
   var element8 = document.createElement('input');            
   element8.type = 'text';            
   element8.name = 'txtRow'+ iteration;
   element8.id = 'txtRow'+ iteration;
   cell8.appendChild(element8);
   element8.size = 20;
   }

function validateLOC(){
   var table = document.getElementById('tblSample');
   var rowCount = table.rows.length;

   for (var i=0; i < rowCount; i++) {
var loc = table.rows[i].cells[1].getElementById('LOCsel')[i];
var strUser = loc.options[loc.selectedIndex].text;      
if (strUser = 'Other'){
   document.getElementById('Comments').disabled = true;
   document.getElementById('Comments').style.backgroundColor = '#cccccc';
   rowCount--;
    i;
}
else {
   document.getElementById('Comments').disabled = false;
   rowCount--;
   i;
}
    return true;
}
 }
 </script>
4

1 回答 1

0

首先通过添加来给出选择框和id:
sel.id = 'LOCsel' + iteration;

然后为每个选择框添加一个 onclick 事件处理程序:
sel.onclick = function () {
onLOCSelected(iteration);
};

然后添加这个函数:
function onLOCSelected(uniqueID)
{
//使用 uniqueID(你称之为迭代)
//评估所选项目的值并相应地切换评论输入
}

希望有帮助!

于 2013-02-27T18:47:58.033 回答