2

这是我的表格:

<form id="Test" action="index.php?course=4" method="post" name="Test" onSubmit="IsFormComplete(12)">  

表格内部是一个动态生成的表格,其中 id=Qx class=Qx 其中 x 是 1 到 12:

 <tr id="Q2" class="Q2">  
            <td width="5%">2) </td>  
            <td colspan="2">According to the Institute of Medicine study the number of deaths from medical errors per year is between</td>  

这是我的javascript函数:

function IsFormComplete(iQuestions) {  
var questionNum = iQuestions;  
    itemOkay=true;  
    for (var i=1;i<questionNum;i++) {  
        for (var j=0;j<4;j++) {  
            var thisItem = eval("document.Test.Q" + i + "[" + j + "].checked");  
            if (!thisItem)  {  
                itemOkay = false;  
                document.getElementById(eval("Q" + i)).style.color = "red";  
            }  
        }  
    }  
    alert("item okay = " + itemOkay);  
    if (itemOkay) {  
        return true;  
    } else {  
        return false;  
    }  

}

不工作请帮助。DOM 新手,尝试过各种标签:

document.getElementById(eval("Q" + i)).style.color = "red";  
document.Test.getElementById(eval("Q" + i)).style.color = "red";  
document.getElementById("Q1").style.color = "red";  //To try literal instead of variable

等等

4

1 回答 1

3

你不需要评估。getElementById 使用字符串。尝试这个:

document.getElementById("Q"+i).style.color = "red";  
于 2012-07-11T10:58:02.883 回答