IE、getElementById 和单选按钮
一些简单的代码:
<input type="radio" id="knob-reassign-leave" name="knob-reassign" value="n" checked="checked">Leave <br>
<input type="radio" id="knob-reassign-cmp" name="knob-reassign" value="d"> Default <br>
<input type="radio" id="knob-reassign" name="knob-reassign" value="r"> Reassign to
<select name="assigned_to" id="assigned_to" onchange="
if ((this.value != 'currentuser') && (this.value != '')) {
var kr = document.getElementById('knob-reassign');
document.getElementById('knob-reassign').checked=true;
}">
<option value="otheruser">Someone Else</option>
<option value="lastuser">Someone Third</option>
<option value="currentuser" selected="selected">Me</option>
</select>
这在 FF 和 chrome 中都运行得很好,但正如我在编写代码时经常听到的那样,这在 IE 中不起作用。看来 IE 正在搜索 ID 的“名称”,或者它尝试将 ID 转换为名称,因为它总是选择第一个无线电,而它应该获取最后一个无线电。
Some simple code:
<input type="radio" id="knob-reassign-leave" name="knob-reassign" value="n" checked="checked">Leave <br>
<input type="radio" id="knob-reassign-cmp" name="knob-reassign" value="d"> Default <br>
<input type="radio" id="knob-reassign" name="knob-reassign" value="r"> Reassign to
<select name="assigned_to" id="assigned_to" onchange="
if ((this.value != 'currentuser') && (this.value != '')) {
var kr = document.getElementById('knob-reassign');
document.getElementById('knob-reassign').checked=true;
}">
<option value="otheruser">Someone Else</option>
<option value="lastuser">Someone Third</option>
<option value="currentuser" selected="selected">Me</option>
</select>
This all works very well in FF and chrome, but as I always hear when I write code, this doesn't work in IE. It appears that IE is searching 'names' for the ID, or it tries to translate the ID into a name, because it always selects the first radio, when it should grab the last.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当名称和 ID 相同时,IE6/7/8 似乎确实选择了第一个
元素。这是一个简单的测试用例:
虽然我无法提供解释(Google 有大量相关结果),我可以提供修复: 覆盖 IE 的 getElementById 实现 或更改最后一个元素的 ID与名称不同:
IE6/7/8 do indeed seem to select the first
<input>
element when the name and ID are the same. Here is a simple test case:While I can't offer an explanation (Google has tons of relevant results), I can offer a fix: Either override IE's implementation of getElementById or change the ID of the last element to be different from the name: