Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript getElementById()返回null,尽管元素存在_Javascript_Jsp_Getelementbyid - Fatal编程技术网

Javascript getElementById()返回null,尽管元素存在

Javascript getElementById()返回null,尽管元素存在,javascript,jsp,getelementbyid,Javascript,Jsp,Getelementbyid,我是网络新手(JSP、java脚本等)。我动态地创建复选框,然后对其应用java脚本。问题是document.getElementById(..)总是返回null。我知道这个问题已经很多次了,我尝试过这些解决方案,但不知怎么的,它们对我不起作用,可能是我尝试错了。这是我的密码: //window.onload = function handleUnusedDieFunctions(checkboxid) { function handleUnusedDieFunctions(checkboxid

我是网络新手(JSP、java脚本等)。我动态地创建复选框,然后对其应用java脚本。问题是document.getElementById(..)总是返回null。我知道这个问题已经很多次了,我尝试过这些解决方案,但不知怎么的,它们对我不起作用,可能是我尝试错了。这是我的密码:

//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++;
我知道在jsp页面中混合java代码不是一个好主意,但这段代码不是我写的,我只需要添加一些功能,但在某个时候这必须得到修复

你能告诉我我做错了什么吗

谢谢。

请删除此语句中的.toString()
var chkd=document.getElementById(checkboxid.toString())
并查看您是否能够访问输入标记。或者,您可以尝试使用如下标记名访问输入标记:
document。getElementsByTagName('input')[0]
这将允许您访问第一个输入标记

编辑:


对于动态生成的元素,我们可以使用addEventListener。您只需要添加一个包装器div标记。我已经用包装器div标记更新了jsFiddle:jsFiddle.net/giri_jeedigunta/NG3cP。请看一下。这对所有动态添加的元素都非常有效。

在“var chkd=document.getElementById(checkboxid.toString());”中执行toString的目的是什么?我需要知道哪些“复选框”我必须在数据库中做一些更改。谢谢您的回复,但这两个解决方案都不起作用:/I已经在jsFiddle上设置了类似的函数:请看一看。无论是否使用。toStrong(),都可以正常工作。另外,我会要求您查看HTML,看看所需的ID是否正确填充和传递。非常感谢:),如果您有静态元素(您知道页面加载前的元素数量),那么它可以工作,但问题是当您有动态元素时(元素数量取决于从数据库检索到的某个值),现在,javascript没有按预期应用。“chkd”的值总是为空。我已经查看了HTML,它似乎是正确的,因为我可以在日志中看到“checkboxid”,但文档中的getElementById(checkboxid)仍然返回null。对于动态生成的元素,我们可以使用addEventListener。您只需要添加一个包装器div标记。我已经用包装器div标记更新了jsFiddle:请看一下。这在所有动态添加的元素上都非常有效。我发现了另一个JSFIDLE,其中动态添加了元素:非常感谢,它成功了,你真的救了我一天:)。如果您可以编辑答案,我可以将其标记为“已接受”。
TypeError: checkboxid is undefined
var chkd = document.getElementById(checkboxid.toString());

ReferenceError: handleUnusedDieFunctions is not defined
handleUnusedDieFunctions('test_1')