Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何对jquery映射值求和_Javascript_Jquery - Fatal编程技术网

Javascript 如何对jquery映射值求和

Javascript 如何对jquery映射值求和,javascript,jquery,Javascript,Jquery,我有map函数来获取选中的复选框,我需要对这个map的值求和,但是我得到了NaN 密码 另一方面,我的附加数据如下所示: 5,467,457NaN57,55805,467,4570 只要我使用复选框,它就会保持这种状态 问题 如何将函数的总和设置为+nf.format(prices)+ 您正在将数组传递给nf.format(number) 而是这样做: function add(a, b) { return a + b; } var number = prices.reduce(ad

我有map函数来获取选中的复选框,我需要对这个map的值求和,但是我得到了
NaN

密码 另一方面,我的附加数据如下所示:

5,467,457NaN57,55805,467,4570
只要我使用复选框,它就会保持这种状态

问题
  • 如何将函数的总和设置为
    +nf.format(prices)+

  • 您正在将数组传递给nf.format(number)

    而是这样做:

    function add(a, b) {
        return a + b;
    }
    
    var number = prices.reduce(add, 0);
    

    然后做nf.格式(数字)

    您的方法似乎是正确的,但我必须说jquery
    map
    的工作方式很奇怪,这些返回数组让我思考了一会儿,但后来我意识到是map在数组中返回值,因此返回了多个数组。我能够使用纯javascript和Mihir建议获得复选框的总和。看起来是这样的

    const checkboxes = document.querySelectorAll('input[type=checkbox]');
    
    // this could have been avoided if NodeList had map method
    const prices = [];
    
    checkboxes.forEach(chkbox => prices.push(chkbox.checked && Number(chkbox.value)));
    
    const totalSum = prices.reduce((a, b) => a + b, 0);
    
    console.log(totalSum)
    
    我测试了几次,似乎效果很好

    试试看:

    var prices = $("input:checkbox:checked").map(function(){
      return $(this).val();
    }).get();
    
    function formatCurr(arr){
      var n = 0;
      if(arr.length){
        $.each(arr, function(k, v){
          n += parseFloat(v);
        });
      }
      return n.toLocaleString('en-US');
    }
    
    $('.paybutton').append('<a class=" btn-link" href="">'+ formatCurr(prices) +'</a>');
    

    但我认为jquery增加了不必要的复杂性,我将您的建议与简单的jsthis结合使用。它将并排返回我的价格,而不是求和
    54674575525015
    5467457是我第一次选择的价格,5525015是我第一次和第二次选择的价格之和。好的,我实际上需要
    $('.paybutton').empty()以清除旧数据并用新数据替换。现在很好。谢谢。它有奇怪的返回数据
    5467457552501554674570
    它同时添加复选框和未选中框。
    const checkboxes = document.querySelectorAll('input[type=checkbox]');
    
    // this could have been avoided if NodeList had map method
    const prices = [];
    
    checkboxes.forEach(chkbox => prices.push(chkbox.checked && Number(chkbox.value)));
    
    const totalSum = prices.reduce((a, b) => a + b, 0);
    
    console.log(totalSum)
    
    var prices = $("input:checkbox:checked").map(function(){
      return $(this).val();
    }).get();
    
    function formatCurr(arr){
      var n = 0;
      if(arr.length){
        $.each(arr, function(k, v){
          n += parseFloat(v);
        });
      }
      return n.toLocaleString('en-US');
    }
    
    $('.paybutton').append('<a class=" btn-link" href="">'+ formatCurr(prices) +'</a>');