get all the input value and make an addition

后端 未结 2 2099
走了就别回头了
走了就别回头了 2021-01-23 05:15
    
  • 2条回答
    •  天命终不由人
      2021-01-23 06:12

      What you want is this:

      $(document).ready(function() { //wrap in a document.ready event handler
          $('input').on('keyup', function() { //bind using jQuery
              var result = 0;
              $('.liste_couleur_qty li input').each(function() {
                  result += parseInt(this.value, 10);
              });
              $('div#qtyvalue').text(result); //result.value doesn't exist, use result.
          });
      });​
      

      Here's a demo: http://jsfiddle.net/jeRdA/

      UDPATE:

      To allow for users changing the value of any of the inputs to ''(e.g., blank, or empty) or a non-numeric value, modify the line:

      result += parseInt(this.value, 10);
      

      to:

      result += parseFloat(this.value, 10) || 0;
      

      Updated fiddle: http://jsfiddle.net/jeRdA/3/

    提交回复
    热议问题