我是Web编程新手(JSP,JavaScript等)。我正在动态创建复选框,然后在它们上应用JavaScript。问题是document.getElementById(..)总是返回null。我知道这个问题已经被问过很多次了,我尝试了那些解决方案,但不知为何它们对我没起作用,可能是我使用方法不对。以下是我的代码:
发生的情况是,我可以在日志和第一个警报中看到文本框id,但document.getElementById(...)返回null。 我尝试将函数放在{{link1:window.load}}之后,但当我这样做时,我甚至没有获得第一个警报,并在控制台上获得这些错误。
//window.onload = function handleUnusedDieFunctions(checkboxid) {
function handleUnusedDieFunctions(checkboxid) {
console.log("checkbox id: " + checkboxid);
var id = checkboxid;
var chkd = document.getElementById(checkboxid.toString());
console.log(chkd);
alert("Just a formality");
if(chkd.checked) {
alert("checked");
}
else {
alert("unchecked");
}
}
<!-- Some portion of jsp code -->
<div .....
out.println("<table>");
if((die.getFunctionList() != null) && (!die.getFunctionList().isEmpty())){
functionListForDie = die.getFunctionList();
ListIterator li11 = functionListForDie.listIterator();
Function f1;
while (li11.hasNext()) {
f1 = (Function)li11.next();
System.out.println("IC: " + ic.getID() + " Die: " + die.getID() + ", Func: " +
f1.getID() + "\n");
out.println("<tr>");
out.println("<td style='width:30px;'>");
if(unusedIcDieList.size() != 0)
{
Boolean sameDie = false;
Boolean sameFunc = false;
for(IcDie icdie: unusedIcDieList)
{
if((icdie.getDieID() == die.getID()))
{
sameDie = true;
System.out.println("icdie.getDieID() == " + icdie.getDieID() +
" , die.getID() == " + die.getID());
ArrayList<Integer> unusedFunctionsList = icdie.getUnusedFunctions();
for(Integer func: unusedFunctionsList)
{
System.out.println("Checking func = " + func + ", f1.getID() = " + f1.getID() );
if(func.intValue() == f1.getID().intValue()) {
System.out.println("func == " + func + " , f1.getID() == " + f1.getID());
sameFunc = true;
}
}
}
}
System.out.println("Same Die: " + sameDie.toString() + " , Same Func: " + sameFunc.toString() + "\n");
if(sameDie && sameFunc) {
String id="test_" + String.valueOf(count);
System.out.println("Should print unchecked checkbox for id: " + id);
%>
<input type="checkbox" name="unusedfunction" class="standard_checkbox"
style="margin: 0px 12px 0px 0px;" id="'<%=id%>'" click="handleUnusedDieFunctions('<%=id%>')" ><br>
<%
}
else {
String id="test_" + String.valueOf(count);
System.out.println("Should print checked checkbox for id: " + id);
%>
<input type="checkbox" name="unusedfunction" class="standard_checkbox" style="margin: 0px 12px 0px 0px;"
id="'<%=id%>'" onclick="handleUnusedDieFunctions('<%=id%>')" checked="checked" ><br>
<%
}
}
else
{
%>
<input type="checkbox" name="unusedfunction" class="standard_checkbox" style="margin: 0px 12px 0px 0px;"
id="'<%=id%>'" onclick="handleUnusedDieFunctions('<%=id%>')" checked="checked" ><br>
<%
}
count++;
发生的情况是,我可以在日志和第一个警报中看到文本框id,但document.getElementById(...)返回null。 我尝试将函数放在{{link1:window.load}}之后,但当我这样做时,我甚至没有获得第一个警报,并在控制台上获得这些错误。
TypeError: checkboxid is undefined
var chkd = document.getElementById(checkboxid.toString());
ReferenceError: handleUnusedDieFunctions is not defined
handleUnusedDieFunctions('test_1')
我知道在JSP页面中混合Java代码不是一个好主意,但这段代码不是由我编写的,我只需要添加一些功能,但是在某个时候必须修复它。
请问我做错了什么?
谢谢。