Home >Web Front-end >JS Tutorial >JavaScript code to implement checkbox selection_javascript skills
Code:
function checkall(checkNames){ var allBoxs = document.getElementsByName(checkNames); for(var i = 0 ;i<allBoxs.length;i++){ if(allBoxs[i].type == 'checkbox'){ if(allBoxs[i].checked==false){ allBoxs[i].checked = true ; }else{ allBoxs[i].checked = false ; } } } }
How to use①
Note: 'goods' is the name of all checkboxes that need to be selected
How to use②
<a href="" id="checkAll">全选</a> <script> document.getElementById('checkAll').onclick = function () { checkall('goods') }; </script>
Note: 'goods' is the name of all checkboxes that need to be selected
The above two methods are all summarized for you in this article. I hope you will like it. If there is a better method, please let me know. This article will continue to be updated.