Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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_Php_Jquery - Fatal编程技术网

Javascript 使用Jquery复选框计算值的结果错误

Javascript 使用Jquery复选框计算值的结果错误,javascript,php,jquery,Javascript,Php,Jquery,现在我正在使用JQuery和PHP代码计算复选框值。其机制是,当用户选中复选框时,它将对价格求和。我为JQuery实现了一个公式,但结果不正确。这是我的密码 JQUERY <script> $(function() { $('.dealprice').on('input', function(){ const getItemPrice = $(this).closest("tr").find('input[name=itemprice

现在我正在使用JQuery和PHP代码计算复选框值。其机制是,当用户选中复选框时,它将对价格求和。我为JQuery实现了一个公式,但结果不正确。这是我的密码

JQUERY

<script>
$(function() {
      $('.dealprice').on('input', function(){

        const getItemPrice = $(this).closest("tr").find('input[name=itemprice]').val(); 

        var eachPrice = 0;
        $('.dealprice:checkbox:checked').each(function(){
            eachPrice += isNaN(parseInt(getItemPrice)) ? 0 : parseInt(getItemPrice);
        }); 
        $("#totalDeal").text(eachPrice.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'));
        
    });
 });
    </script>

$(函数(){
$('.dealprice')。在('input',function()上{
const getItemPrice=$(this.closest(“tr”).find('input[name=itemprice]')).val();
var-eachPrice=0;
$('.dealprice:checkbox:checked')。每个(函数(){
eachPrice+=isNaN(parseInt(getItemPrice))?0:parseInt(getItemPrice);
}); 
$(“#totalDeal”).text(eachPrice.toFixed(2).replace(/\d(?=(\d{3})+\)/g,'$&');
});
});
更多细节。我在这个网站上做了一个示例,只需单击“运行”按钮,您就可以对其进行测试。我需要按照表格格式显示正确的计算结果


请提供帮助。

您只需在循环中定义getItemPrice,否则您只需为单击的项目计算一次,而不是为每个项目计算一次

$(函数(){
$('.dealprice')。在('input',function()上{
var-eachPrice=0;
$('.dealprice:checkbox:checked')。每个(函数(){
const getItemPrice=$(this.closest(“tr”).find('input[name=itemprice]')).val();
eachPrice+=isNaN(parseInt(getItemPrice))?0:parseInt(getItemPrice);
}); 
$(“#totalDeal”).text(eachPrice.toFixed(2).replace(/\d(?=(\d{3})+\)/g,'$&');
});
});

项目
价格
处理
书
$ 10 
铅笔
$ 5 
笔
$ 8 
全部的
0

您必须将
getItemPrice
放在函数的内部,以便检查它。请检查以下代码:

$(function() {
  $('.dealprice').on('input', function(){
    var eachPrice = 0;
    $('.dealprice:checkbox:checked').each(function(){
      const getItemPrice = $(this).closest("tr").find('input[name=itemprice]').val();
      eachPrice += isNaN(parseInt(getItemPrice)) ? 0 : parseInt(getItemPrice);
    }); 
    $("#totalDeal").text(eachPrice.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'));
  });
});

也请检查此回复

能否附上html或创建最小复制示例。非常感谢您的回答。真的很有帮助