document.getElementById 有时会抓name放过了id ,据说是IE的一个BUG;
http://community.csdn.net/Expert/topic/4223/4223888.xml?temp=.1947443
页面中有
<input type="hidden" id="hello8" name="category_id" value="2" />
< HTML >
< HEAD >
< TITLE > getElementById </ TITLE >
</ HEAD >
< SCRIPT LANGUAGE ="JavaScript" >
<!--
function chkacc(){
alert(document.getElementById("tbxuid1").value);
alert(document.getElementById("tbxpwd1").value);
alert(document.getElementById("tbxuid").value);
alert(document.getElementById("tbxpwd").value);
}
//-->
</ SCRIPT >
< BODY >
< FORM METHOD =POST ACTION ="" name ="frm1" >
< INPUT TYPE ="text" NAME ="tbxuid" id ="tbxuid1" >
< INPUT TYPE ="text" NAME ="tbxpwd" id ="tbxpwd1" >
</ FORM >
< FORM METHOD =POST ACTION ="" name ="frm2" >
< INPUT TYPE ="text" NAME ="tbxuid" id ="tbxuid" >
< INPUT TYPE ="text" NAME ="tbxpwd" id ="tbxpwd" >
< INPUT TYPE ="button" value ="檢查" name ="btnchk" onclick ="chkacc();" >
</ FORM >
</ BODY >
</ HTML
http://community.csdn.net/Expert/topic/4223/4223888.xml?temp=.1947443
页面中有
<input type="hidden" id="hello8" name="category_id" value="2" />
<select id="category_id" οnchange="al();">
一个是name="category_id" 一个是id="category_id"
用document.getElementById取第二个,可是,取到的却是第一个name=category_id
在IE中getElementById竟然不是先抓id而是先找name相同的物件...
兩個form,每個form有兩個textbox,兩個form中的textbox是相同的name,但id都不同...
這樣在Firefox是沒問題的...但在IE卻只抓得到第一個出現的name資料
下面这段代码可以验证这个结果
< HTML >
< HEAD >
< TITLE > getElementById </ TITLE >
</ HEAD >
< SCRIPT LANGUAGE ="JavaScript" >
<!--
function chkacc(){
alert(document.getElementById("tbxuid1").value);
alert(document.getElementById("tbxpwd1").value);
alert(document.getElementById("tbxuid").value);
alert(document.getElementById("tbxpwd").value);
}
//-->
</ SCRIPT >
< BODY >
< FORM METHOD =POST ACTION ="" name ="frm1" >
< INPUT TYPE ="text" NAME ="tbxuid" id ="tbxuid1" >
< INPUT TYPE ="text" NAME ="tbxpwd" id ="tbxpwd1" >
</ FORM >
< FORM METHOD =POST ACTION ="" name ="frm2" >
< INPUT TYPE ="text" NAME ="tbxuid" id ="tbxuid" >
< INPUT TYPE ="text" NAME ="tbxpwd" id ="tbxpwd" >
< INPUT TYPE ="button" value ="檢查" name ="btnchk" onclick ="chkacc();" >
</ FORM >
</ BODY >
</ HTML