检查动态创建的下拉框的选定值

时间:2013-02-27 18:32:06

标签: html-select

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

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

<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>

1 个答案:

答案 0 :(得分:0)

首先添加选择框和id:
sel.id ='LOCsel'+迭代;

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

然后添加此功能:
function onLOCSelected(uniqueID)
{
    //使用uniqueID(你调用它的迭代)
    //评估所选项目的价值&amp;相应地切换评论输入
}

希望有所帮助!