get all the input value and make an addition

后端 未结 2 2073
走了就别回头了
走了就别回头了 2021-01-23 05:15
    
  • 2条回答
    •  南笙
      南笙 (楼主)
      2021-01-23 05:53

      Try this:

      var $inputs = $('.liste_couleur_qty li input');
      $inputs.keyup(function() {
         var result = 0;
         $inputs.each(function(){
           result += parseInt(this.value, 10);
         });
         $('#qtyvalue').html(result);    
      });
      

      This would need to be in a document ready handler or in a script block after the elements in question (many people put their scripts at the end of the body).

      In your code, input.onkeyup = ... wouldn't work because there is no variable input (it doesn't automatically pick up all input elements), and also using someElement.onkeyup = only lets you attach a handler to one element at a time. Use jQuery to bind the keyup handler instead. Also, to use the total at the end just use result, using result.value wouldn't work.

    提交回复
    热议问题