js购物车的全选和反选

购物车的全选功能

   checkAll.onclick = function () {
        var status = this.checked;
        console.log(status);

        for (var i = 0; i < checkOneList.length; i++) {
            var checkOne = checkOneList[i];
            checkOne.checked = status;
        }
    }
购物车的反选功能  (所有的单选框都被勾选 才可以反选 ,有一个没有就不行) 

// 假设法
 for (let i = 0; i < checkOneList.length; i++) {
       let checkOne = checkOneList[i];
      checkOne.onclick = function () {
           var flag = true;  // 假设全都选中
           for (var j = 0; j < checkOneList.length; j++) {
             var each = checkOneList[j];
              if (!each.checked) { // each.checked == false
                flag = false;
                  break;
            }
          }

          checkAll.checked = flag;

       }
     }
// 计数法
 for (let i = 0; i < checkOneList.length; i++) {
        let checkOne = checkOneList[i];
        checkOne.onclick = function () {
            var count = 0;  // 选中的单选框的数量
            for (var j = 0; j < checkOneList.length; j++) {
                var each = checkOneList[j];
                if (each.checked) {
                    count++;
                }
            }
            if (count == checkOneList.length) {
                checkAll.checked = true;
            } else {
                checkAll.checked = false;
            }


        }
    }

你可能感兴趣的:(JS)