如下

<!DOCTYPE HTML>
<head>
	<meta charset="utf-8" />
</head>
<body>
<input type="checkbox" />
<input type="checkbox" checked/>

<script>
	var checkboxs = document.getElementsByTagName('input');
	alert(checkboxs[0].getAttribute('checked'));
	alert(checkboxs[1].getAttribute('checked'));
</script>
</body>
</HTML>

IE6/7 :依次返回 false/true

IE8 :依次返回 空字符串/checked

IE9/10/Firefox/Safari/Chrome/Opera :依次返回 null/空字符串

 

input[type=radio]的情况同上,类似的布尔属性还有:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected

注意:要判断checkbox/radio有没有选定,使用property而不要使用attribute。

相关文章:

  • 2021-10-09
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
猜你喜欢
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
相关资源
相似解决方案