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'+反復;

次に、各選択ボックスにonclickイベントハンドラーを追加します
。sel.onclick= function(){
onLOCSelected(iteration);
};

次に、次の関数を追加し
ます。function onLOCSelected(uniqueID)
{
// uniqueIDを使用(呼び出したとおりの反復)
//選択したアイテムの値を評価し、それに応じて入力されたコメントを切り替えます
}

お役に立てば幸いです。

于 2013-02-27T18:47:58.033 に答える