Javascript 试图访问输入类型时getElementById为null=";“隐藏的”;
我在使用getElementById访问Javascript 试图访问输入类型时getElementById为null=";“隐藏的”;,javascript,Javascript,我在使用getElementById访问并获取其值方面遇到了一个问题 这是我的功能,它位于第1页,用于显示我的选择,并让我返回并更改它们 function showTerrain(terrain) { if(document.getElementById('muni1') == null){ alert('is null'); } else {alert('test:' + document.getElementById('muni1').value);} w = window.ope
并获取其值方面遇到了一个问题
这是我的功能,它位于第1页,用于显示我的选择,并让我返回并更改它们
function showTerrain(terrain) {
if(document.getElementById('muni1') == null){ alert('is null'); }
else {alert('test:' + document.getElementById('muni1').value);}
w = window.open("terrainChooser.php?terr=" + terrain + "®ID=1&muni=Rouyn-Noranda&parc=193", "", "width=500,height=450");
}
当showTerrain被激活时,它会被发送到terrainChooser.php,它让我选择3个值并返回如下信息:
window.opener.document.getElementById(elementID).innerHTML =
"<input readonly type='text' style='background-color:#eed8bb;border:0;' name='parcName" + parcNum + "' value=\"" + txt + "\" />
<input type='hidden' name='parcId" +parcNum + "' value='" + id + "' />
<input type='hidden' name='regID" + parcNum + "' value='" + regID + "' />
<input type='hidden' name='muni" + parcNum + "' value='" + muni +"' />";
window.opener.document.getElementById(elementID).innerHTML=
"
";
我知道我搜索的Id是唯一的,因为它在浏览器中是唯一的(firebug):
不知道该怎么办。。。提前谢谢 函数“getElementById()”对元素id值进行操作,而不是对元素name值进行操作。(好吧,Internet Explorer认为“名称”和“id”的意思是一样的,但这完全是错误的。其他浏览器不同意。)
给元素一个“id”:
您设置的是“名称”而不是“id”您使用的是
name
属性,而不是id
属性。改变
name='parcId" +parcNum + "'
到
它应该会起作用
document.getElementById(elementID)
必须指向元素的ID字段,我相信您指向的是name属性。这只适用于IE
<input type="hidden" value="Rouyn-Noranda" name="muni1"></td>
需要
<input type="hidden" value="Rouyn-Noranda" id="muni1"></td>
也许我是瞎子,但是。。。在html代码中,没有id为“muni1”的元素,只有一个名为的元素。更改:
<input type="hidden" value="Rouyn-Noranda" name="muni1">
致:
非常感谢,我没有意识到我没有发送id。
<input type="hidden" value="Rouyn-Noranda" name="muni1"></td>
<input type="hidden" value="Rouyn-Noranda" id="muni1"></td>
<input type="hidden" value="Rouyn-Noranda" name="muni1">
<input type="hidden" value="Rouyn-Noranda" name="muni1" id="muni1">