jQuery sum checkbox values

前端 未结 2 835
迷失自我
迷失自我 2020-12-22 12:50

I\'d need to calculate sum of all checkboxes and update total based on selection here is html:



        
2条回答
  •  甜味超标
    2020-12-22 13:24

    You can use .not() to exclude #total element value from calculations, .get() , Array.prototype.reduce() , + operator to cast value of :checkbox to Number

    $(":checkbox").change(function(event) {
      $("#total").val(function() {
        return $(event.target.tagName).not("[type=text]")
        .get().reduce(function(a, b) {
            return +a.value + +b.value
        })
      })
    })
    
    
    
    

提交回复
热议问题