Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.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不断地把总数加起来_Javascript_Jquery - Fatal编程技术网

Javascript不断地把总数加起来

Javascript不断地把总数加起来,javascript,jquery,Javascript,Jquery,我正在尝试将一些表单字段相加,但我尝试的数组并不重要,如果再次选中,它会不断将总值相加,我需要它将复选框中的值与金额中的输入值相加,如果选中,则仅当未选中的值减去或不加时,我非常感谢你的帮助 <div class="hire-accessories"><input type="checkbox" name="baby-st" id="baby-st" class="checkbox" value="5"/>Baby Seat 0-2 Year £5(Day)</di

我正在尝试将一些表单字段相加,但我尝试的数组并不重要,如果再次选中,它会不断将总值相加,我需要它将复选框中的值与金额中的输入值相加,如果选中,则仅当未选中的值减去或不加时,我非常感谢你的帮助

<div class="hire-accessories"><input type="checkbox" name="baby-st" id="baby-st" class="checkbox" value="5"/>Baby Seat 0-2 Year £5(Day)</div>
<div class="hire-accessories"><input type="checkbox" name="child-st" id="child-st" class="checkbox" value="5" />Child Seat 3-7 Year Seat £7(Day)</div>
<div class="hire-accessories"><input type="checkbox" name="gps" id="gps" class="checkbox" value="5" />TomTom GPS</div>
<div class="hire-accessories"><input type="checkbox" name="bike-rack" id="bike-rack" class="checkbox" value="10" />Bike Rack</div>
<div class="hire-accessories"><input type="checkbox" name="mbike" id="mbike" class="checkbox" value="5" />Montainbike</div>

<input type="text" name="Amount" id="Amount" value="20" readonly class="cat_textbox" />
改变

document.getElementById('Amount').value = Amount+total;
将来

更新金额值时,您意外地将原始的
金额
值与
总额
一起添加

如果您想在总额中添加其他内容(附加费?其他购买?税费?),您需要向我们提供更多信息,然后我们才能告诉您最好的方法。但总体思路是将该值存储在变量中:

var otherAmount = 20;
然后像前面一样添加它:

document.getElementById('Amount').value = total + otherAmount;
(您无法将其他金额存储在#amount.value中,这是您正在尝试执行的操作。)

更改

document.getElementById('Amount').value = Amount+total;
将来

更新金额值时,您意外地将原始的
金额
值与
总额
一起添加

如果您想在总额中添加其他内容(附加费?其他购买?税费?),您需要向我们提供更多信息,然后我们才能告诉您最好的方法。但总体思路是将该值存储在变量中:

var otherAmount = 20;
然后像前面一样添加它:

document.getElementById('Amount').value = total + otherAmount;
(您无法将其他金额存储在#amount.value中,这是您正在尝试执行的操作。)

更改

document.getElementById('Amount').value = Amount+total;
将来

更新金额值时,您意外地将原始的
金额
值与
总额
一起添加

如果您想在总额中添加其他内容(附加费?其他购买?税费?),您需要向我们提供更多信息,然后我们才能告诉您最好的方法。但总体思路是将该值存储在变量中:

var otherAmount = 20;
然后像前面一样添加它:

document.getElementById('Amount').value = total + otherAmount;
(您无法将其他金额存储在#amount.value中,这是您正在尝试执行的操作。)

更改

document.getElementById('Amount').value = Amount+total;
将来

更新金额值时,您意外地将原始的
金额
值与
总额
一起添加

如果您想在总额中添加其他内容(附加费?其他购买?税费?),您需要向我们提供更多信息,然后我们才能告诉您最好的方法。但总体思路是将该值存储在变量中:

var otherAmount = 20;
然后像前面一样添加它:

document.getElementById('Amount').value = total + otherAmount;

(您无法将其他金额存储在#amount.value中,这正是您尝试执行的操作。)

它不断添加,因为您在此处提取新的金额值:

var Amount = parseFloat(document.getElementById("Amount").value);
如果要在原始值20的基础上添加总和,则必须执行以下操作:

它不断增加,因为您在此处提取新的金额值:

var Amount = parseFloat(document.getElementById("Amount").value);
如果要在原始值20的基础上添加总和,则必须执行以下操作:

它不断增加,因为您在此处提取新的金额值:

var Amount = parseFloat(document.getElementById("Amount").value);
如果要在原始值20的基础上添加总和,则必须执行以下操作:

它不断增加,因为您在此处提取新的金额值:

var Amount = parseFloat(document.getElementById("Amount").value);
如果要在原始值20的基础上添加总和,则必须执行以下操作:

将您的金额声明移到updateTotal函数之外

var Amount = parseFloat(document.getElementById("Amount").value);
var updateTotal = function () { 
    var total = 0;

    total += ($('input[name=baby-st]:checked').length > 0) ? parseFloat($('input[name=baby-st]:checked').val()) : 0;
    total += ($('input[name=child-st]:checked').length > 0) ? parseFloat($('input[name=child-st]:checked').val()) : 0;
    total += ($('input[name=gps]:checked').length > 0) ? parseFloat($('input[name=gps]:checked').val()) : 0;
    total += ($('input[name=bike-rack]:checked').length > 0) ? parseFloat($('input[name=bike-rack]:checked').val()) : 0;
    total += ($('input[name=mbike]:checked').length > 0) ? parseFloat($('input[name=mbike]:checked').val()) : 0;

    document.getElementById('Amount').value = Amount+total;

};

$('#baby-st, #child-st, #gps, #bike-rack, #mbike').change(function() {

    if(this.checked)
    {

    }
    updateTotal();

});

将您的金额声明移到updateTotal函数之外

var Amount = parseFloat(document.getElementById("Amount").value);
var updateTotal = function () { 
    var total = 0;

    total += ($('input[name=baby-st]:checked').length > 0) ? parseFloat($('input[name=baby-st]:checked').val()) : 0;
    total += ($('input[name=child-st]:checked').length > 0) ? parseFloat($('input[name=child-st]:checked').val()) : 0;
    total += ($('input[name=gps]:checked').length > 0) ? parseFloat($('input[name=gps]:checked').val()) : 0;
    total += ($('input[name=bike-rack]:checked').length > 0) ? parseFloat($('input[name=bike-rack]:checked').val()) : 0;
    total += ($('input[name=mbike]:checked').length > 0) ? parseFloat($('input[name=mbike]:checked').val()) : 0;

    document.getElementById('Amount').value = Amount+total;

};

$('#baby-st, #child-st, #gps, #bike-rack, #mbike').change(function() {

    if(this.checked)
    {

    }
    updateTotal();

});

将您的金额声明移到updateTotal函数之外

var Amount = parseFloat(document.getElementById("Amount").value);
var updateTotal = function () { 
    var total = 0;

    total += ($('input[name=baby-st]:checked').length > 0) ? parseFloat($('input[name=baby-st]:checked').val()) : 0;
    total += ($('input[name=child-st]:checked').length > 0) ? parseFloat($('input[name=child-st]:checked').val()) : 0;
    total += ($('input[name=gps]:checked').length > 0) ? parseFloat($('input[name=gps]:checked').val()) : 0;
    total += ($('input[name=bike-rack]:checked').length > 0) ? parseFloat($('input[name=bike-rack]:checked').val()) : 0;
    total += ($('input[name=mbike]:checked').length > 0) ? parseFloat($('input[name=mbike]:checked').val()) : 0;

    document.getElementById('Amount').value = Amount+total;

};

$('#baby-st, #child-st, #gps, #bike-rack, #mbike').change(function() {

    if(this.checked)
    {

    }
    updateTotal();

});

将您的金额声明移到updateTotal函数之外

var Amount = parseFloat(document.getElementById("Amount").value);
var updateTotal = function () { 
    var total = 0;

    total += ($('input[name=baby-st]:checked').length > 0) ? parseFloat($('input[name=baby-st]:checked').val()) : 0;
    total += ($('input[name=child-st]:checked').length > 0) ? parseFloat($('input[name=child-st]:checked').val()) : 0;
    total += ($('input[name=gps]:checked').length > 0) ? parseFloat($('input[name=gps]:checked').val()) : 0;
    total += ($('input[name=bike-rack]:checked').length > 0) ? parseFloat($('input[name=bike-rack]:checked').val()) : 0;
    total += ($('input[name=mbike]:checked').length > 0) ? parseFloat($('input[name=mbike]:checked').val()) : 0;

    document.getElementById('Amount').value = Amount+total;

};

$('#baby-st, #child-st, #gps, #bike-rack, #mbike').change(function() {

    if(this.checked)
    {

    }
    updateTotal();

});

首先,需要从函数中提取amount变量,这样您就不会每次更改事件都得到更新的值。 也。。。。。。我可以建议您在示例中减少不必要的代码吗

// Use parseInt everywhere instead of parseFloat. You're not
// supplying floating integers, so no need to parse them as one
var amount = parseInt(document.getElementById("Amount").value);
var updateTotal = function() {
  var total = 0;

  // Loop through the inputs since they all share the same class
  $('.hire-accessories input:checked').each(function() {
    total += parseInt($(this).val());
  });

  document.getElementById('Amount').value = amount + total;

};

// Pass the function directly to the event listener
// the anonymous function and random conditional are unnecessary
$('.hire-accessories input').change(updateTotal);

首先,需要从函数中提取amount变量,这样您就不会每次更改事件都得到更新的值。 也。。。。。。我可以建议您在示例中减少不必要的代码吗

// Use parseInt everywhere instead of parseFloat. You're not
// supplying floating integers, so no need to parse them as one
var amount = parseInt(document.getElementById("Amount").value);
var updateTotal = function() {
  var total = 0;

  // Loop through the inputs since they all share the same class
  $('.hire-accessories input:checked').each(function() {
    total += parseInt($(this).val());
  });

  document.getElementById('Amount').value = amount + total;

};

// Pass the function directly to the event listener
// the anonymous function and random conditional are unnecessary
$('.hire-accessories input').change(updateTotal);

首先,需要从函数中提取amount变量,这样您就不会每次更改事件都得到更新的值。 也。。。。。。我可以建议您在示例中减少不必要的代码吗

// Use parseInt everywhere instead of parseFloat. You're not
// supplying floating integers, so no need to parse them as one
var amount = parseInt(document.getElementById("Amount").value);
var updateTotal = function() {
  var total = 0;

  // Loop through the inputs since they all share the same class
  $('.hire-accessories input:checked').each(function() {
    total += parseInt($(this).val());
  });

  document.getElementById('Amount').value = amount + total;

};

// Pass the function directly to the event listener
// the anonymous function and random conditional are unnecessary
$('.hire-accessories input').change(updateTotal);

首先,需要从函数中提取amount变量,这样您就不会每次更改事件都得到更新的值。 也。。。。。。我可以建议您在示例中减少不必要的代码吗

// Use parseInt everywhere instead of parseFloat. You're not
// supplying floating integers, so no need to parse them as one
var amount = parseInt(document.getElementById("Amount").value);
var updateTotal = function() {
  var total = 0;

  // Loop through the inputs since they all share the same class
  $('.hire-accessories input:checked').each(function() {
    total += parseInt($(this).val());
  });

  document.getElementById('Amount').value = amount + total;

};

// Pass the function directly to the event listener
// the anonymous function and random conditional are unnecessary
$('.hire-accessories input').change(updateTotal);

以下是一个工作版本:

首先需要在函数之外设置“var金额”。然后,在进行更改时,如果您希望从当前复选框中重新配置总额,请返回该值并将其添加到原始基本金额中

//Moved this out of the function
var Amount = parseFloat($('#Amount').val());
var updateTotal = function () { 

var total = 0;

total += ($('input[name=baby-st]:checked').length > 0) ? parseFloat($('input[name=baby-st]:checked').val()) : 0;
total += ($('input[name=child-st]:checked').length > 0) ? parseFloat($('input[name=child-st]:checked').val()) : 0;
total += ($('input[name=gps]:checked').length > 0) ? parseFloat($('input[name=gps]:checked').val()) : 0;
total += ($('input[name=bike-rack]:checked').length > 0) ? parseFloat($('input[name=bike-rack]:checked').val()) : 0;
total += ($('input[name=mbike]:checked').length > 0) ? parseFloat($('input[name=mbike]:checked').val()) : 0;
    //Return your total
    return total;     
};

$('#baby-st, #child-st, #gps, #bike-rack, #mbike').change(function() {

    if(this.checked)
   {



   }
   var total = updateTotal();
   //Reset the amount to display
   $('#Amount').val(Amount+total);

});

以下是一个工作版本:

首先需要在函数之外设置“var金额”。然后,在进行更改时,如果您希望从当前复选框中重新配置总额,请返回该值并将其添加到原始基本金额中

//Moved this out of the function
var Amount = parseFloat($('#Amount').val());
var updateTotal = function () { 

var total = 0;

total += ($('input[name=baby-st]:checked').length > 0) ? parseFloat($('input[name=baby-st]:checked').val()) : 0;
total += ($('input[name=child-st]:checked').length > 0) ? parseFloat($('input[name=child-st]:checked').val()) : 0;
total += ($('input[name=gps]:checked').length > 0) ? parseFloat($('input[name=gps]:checked').val()) : 0;
total += ($('input[name=bike-rack]:checked').length > 0) ? parseFloat($('input[name=bike-rack]:checked').val()) : 0;
total += ($('input[name=mbike]:checked').length > 0) ? parseFloat($('input[name=mbike]:checked').val()) : 0;
    //Return your total
    return total;     
};

$('#baby-st, #child-st, #gps, #bike-rack, #mbike').change(function() {

    if(this.checked)
   {



   }
   var total = updateTotal();
   //Reset the amount to display
   $('#Amount').val(Amount+total);

});

以下是一个工作版本:

首先需要在函数之外设置“var金额”。然后,在进行更改时,如果您希望从当前复选框中重新配置总额,请返回该值并将其添加到原始基本金额中

//Moved this out of the function
var Amount = parseFloat($('#Amount').val());
var updateTotal = function () { 

var total = 0;

total += ($('input[name=baby-st]:checked').length > 0) ? parseFloat($('input[name=baby-st]:checked').val()) : 0;
total += ($('input[name=child-st]:checked').length > 0) ? parseFloat($('input[name=child-st]:checked').val()) : 0;
total += ($('input[name=gps]:checked').length > 0) ? parseFloat($('input[name=gps]:checked').val()) : 0;
total += ($('input[name=bike-rack]:checked').length > 0) ? parseFloat($('input[name=bike-rack]:checked').val()) : 0;
total += ($('input[name=mbike]:checked').length > 0) ? parseFloat($('input[name=mbike]:checked').val()) : 0;
    //Return your total
    return total;     
};

$('#baby-st, #child-st, #gps, #bike-rack, #mbike').change(function() {

    if(this.checked)
   {



   }
   var total = updateTotal();
   //Reset the amount to display
   $('#Amount').val(Amount+total);

});

以下是一个工作版本:

首先需要在函数之外设置“var金额”。然后,在进行更改时,如果您希望从当前复选框中重新配置总额,请返回该值并将其添加到原始基本金额中

//Moved this out of the function
var Amount = parseFloat($('#Amount').val());
var updateTotal = function () { 

var total = 0;

total += ($('input[name=baby-st]:checked').length > 0) ? parseFloat($('input[name=baby-st]:checked').val()) : 0;
total += ($('input[name=child-st]:checked').length > 0) ? parseFloat($('input[name=child-st]:checked').val()) : 0;
total += ($('input[name=gps]:checked').length > 0) ? parseFloat($('input[name=gps]:checked').val()) : 0;
total += ($('input[name=bike-rack]:checked').length > 0) ? parseFloat($('input[name=bike-rack]:checked').val()) : 0;
total += ($('input[name=mbike]:checked').length > 0) ? parseFloat($('input[name=mbike]:checked').val()) : 0;
    //Return your total
    return total;     
};

$('#baby-st, #child-st, #gps, #bike-rack, #mbike').change(function() {

    if(this.checked)
   {



   }
   var total = updateTotal();
   //Reset the amount to display
   $('#Amount').val(Amount+total);

});

一旦你增加了价值,你就永远不会“取消”它。您的代码总是从检查前一个总数开始。我想如果你忘记了“金额”,它会工作得更好。你总是重置
var-Amount
,所以新的数字会被保留。一旦你添加了一个值,你就应该把你的代码干得太干净利落了,你永远不会“取消添加”它。您的代码总是从检查前一个总数开始。我想如果你忘了“数量”,效果会好得多