选择所有在 IE8 中不起作用的输入
我在此页面上有一个全选按钮,该按钮适用于其他浏览器,但不适用于 IE8,除了查看我的源代码之外,任何人都可以看到问题吗?
更新:
这是我的代码:
<td valign="middle" align="center"><input type="checkbox" name="products-quote[]" value="<?php echo $product_option['id']; ?>" /></td>
<td valign="middle" align="center"><input type="checkbox" name="products-sample[]" value="<?php echo $product_option['id']; ?>" /></td>
<script language="JavaScript">
function toggle(source) {
checkboxes = document.getElementsByName(source.name);
for(var i in checkboxes)
checkboxes[i].checked = source.checked;
}
</script>
<tr>
<td valign="middle" align="center"><input type="checkbox" onClick="toggle(this)" name="products-quote[]" value="0" /></td>
<td valign="middle" align="center"><input type="checkbox" onClick="toggle(this)" name="products-sample[]" value="0" /></td>
<td><p><b>Select all</b></p></td>
</tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不使用 document.forms['myForm'].elements 集合?
Why just not use
document.forms['myForm'].elements
collection?您犯了以下错误:
var
关键字将checkboxes
声明为(本地)变量,这很容易出错。for
-in
语句。for
-in
迭代对象的可枚举属性,但此类宿主对象的属性不需要是可枚举的。事实上,它们具有数字名称的属性(您所追求的)是否是可枚举的,以及它们具有非数字名称的属性是否是可枚举的(!),取决于 DOM 实现。这解释了浏览器之间的差异。始终使用(C 风格)for
语句。更改为:
在本例中未经测试,但通常已得到证实。我删除了演示属性和元素,以便您可以更清楚地看到解决方案。您应该将它们替换为基于 CSS 的格式。
您可能还需要考虑为切换复选框指定不同的名称(以便您不必在客户端迭代和服务器端处理中排除它们),并将复选框组名称作为第二个字符串参数传递给
切换(...)
。You have made the following mistakes:
checkboxes
a (local) variable with thevar
keyword, which is error-prone.NodeList
interface of W3C DOM Level 2+ Core with afor
-in
statement.for
-in
iterates over enumerable properties of an object, but the properties of such host objects do not need to be enumerable. In fact, whether their properties with numeric name (which you were after) are enumerable, and whether their properties with non-numeric name are enumerable(!), depends on the DOM implementation. That accounts for the differences between browsers. Always use a (C-style)for
statement there.Change to:
Untested in this case, but generally proved. I have removed the presentational attributes and elements so that you can see the solution more clearly. You should replace those with CSS-based formatting.
You might also want to consider giving the toggle checkboxes different names (so that you do not have to exclude them in the client-side iteration and server-side processing), and passing the checkbox group name as a second, string argument to
toggle(…)
.使用索引可能是最有效的方法。因为,当我尝试枚举名为复选框的对象(像您一样)时,似乎在不同的浏览器中存储了不同对象的数量。
因此,尝试使用一个循环,直到
checkboxes
的长度为止并对其进行迭代。Using indexes could be the most efficient method. Because, when I tried to enumerate the object named checkboxes (like you), seems to stored number of different objects in the different browsers.
So, try to use a loop that counts until length of
checkboxes
and iterate it.更多帮助和最佳实践:
您可以通过以下元标记(字符集仅用于当前代码的兼容性)强制 IE 使用其边缘渲染引擎,而不是使用您正在使用的元标记
,请注意,实际上并不需要添加 chrome=IE8,但如果您遇到一个 ie8 或更低版本的用户安装了 chorme 框架,它将使用 chrome 高级渲染引擎而不是 ie8
(顺便说一句,你也可以提示他们安装 google 框架 - 但这超出了主题)
根据我的经验,这个 hack 解决了很多问题神秘的IE8问题。
其他一些小问题:
1.您正在使用 language=javascript - 您不再需要它了..更好地使用 type=text/javascript (今天也几乎不需要,也许将来随着咖啡脚本等的兴起而相关)
2.包含脚本桌子里面!为什么?最好包含在 head 中,或者更好(为了性能)包含在主体部分的底部,并带有 $(document).ready 函数,甚至更好地包含在底部,带有 $.ready 并从不同的 js 文件调用以将 js 分开html 的其余部分。
现在是更重要的部分 - 您已经在页面中调用 jQuery - 让它完成繁重的工作!
Jquery 已经针对浏览器间兼容性、性能等进行了优化,使用起来也更简单:
您可以绑定切换事件,而无需“onclick”,例如:为复选框按钮添加“selectAll”类。
现在是脚本:
编辑:看来我在代码中犯了一些错误 - 已修复!就像魅力一样
more help and best practice:
instead of the meta tag tou are using you can force IE to use his edge rendring engine by the following meta tags (the charset just for compatibility for the current code)
notice that adding the chrome=IE8 isn't really needed but if you encounter a user with ie8 or less that installed chorme frame it would use chrome advanced rendering engine instead of ie8
(by the way you could also prompt them to install google frame- but that is out of topic)
from my experience this hack solves a lot of mystery IE8 problems.
a couple of other small problems:
1.you are using language=javascript - you don't need that anymore.. better using type=text/javascript (also almost not needed today, maybe relevant in the future with the rise of coffeescript etc)
2.the script is included inside the table! why? better to include in the head or even better (for performance) in the bottom of the body section with $(document).ready function around or even better in the bottom with $.ready and called from a different js file to seperate js from the rest of the html.
and now for the more important part - you are already calling jQuery in your page - let it do the heavy lifting!
Jquery is already optimized for inter-browser compatibility, performance, etc, it's also simpler to use:
you could bind the toggle event without the "onclick" with something like: adding a 'selectAll' class for the checkbox buttons.
and now for the script:
edit: seems I made few mistakes in the code - fixed! and works like a charm
尝试使用“getElementsByTagName”而不是“getElementsByName”。 getElementsByName 在 IE 中不起作用。
Try 'getElementsByTagName' instead of 'getElementsByName'. getElementsByName doesn't work in IE.