document.getElementById not working

耗尽温柔 提交于 2019-12-07 22:56:27

问题


I have 2 chekboxes on a page. There are wrapped in a table cell each within their own row. Doing a document.getElementById('chk1_FEAS~1005') returns the element but document.getElementById('chk5_STG2~1005') is null. For what reasons could this be happening? (I'm testing in IE 8).

<input id="chk1_FEAS~1005" value="JobStages###StageCode~JobCode###FEAS~1005" onclick="addRemoveRow(this.value,this.checked)" style="border-width:0px;padding:1px;margin:0px;height:14px;"  type="checkbox" />

<input id="chk5_STG2~1005" value="JobStages###StageCode~JobCode###STG2~1005" onclick="addRemoveRow(this.value,this.checked)" style="border-width:0px;padding:1px;margin:0px;height:14px;"  type="checkbox" />

回答1:


Your Id has invalid characters:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

More information here.




回答2:


document.getElementById('hk5_STG2~1005') 

should be

document.getElementById('chk5_STG2~1005') 

:-)




回答3:


Looking at your sample it is a typo, the second item should be, document.getElementById('chk5_STG2~1005')

Also, I would recommend removing the ~ character as it is invalid for an id.




回答4:


For what it's worth, I have similar problems using document.getElementById on checkboxes in IE8. Try adding this tag to your head section :-

<meta http-equiv="X-UA-Compatible" content="IE=7" />

This will force IE8 into IE7 compatibility mode. It works for me as a workaround until I find-out what's really up.



来源:https://stackoverflow.com/questions/1266470/document-getelementbyid-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!