Home  >  Article  >  Web Front-end  >  JavaScript code to implement checkbox selection_javascript skills

JavaScript code to implement checkbox selection_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:01:441260browse

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①

Copy code The code is as follows:

bb81ef2c430849232bbe8b43168b82ccSelect all5db79b134e9f6b82c0b36e0489ee08ed

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn