简体   繁体   English

添加大量输入框

[英]Adding up a load of input boxes

This may have been answered before, but I cannot find a solution that works.这可能以前已经回答过,但我找不到有效的解决方案。

I need to add the subtotal input boxes up and output them as a grandTotal.我需要将小计输入框和 output 添加为总计。 Sounds simple, and I thought it would be, but for some reason I cannot get it to function properly.听起来很简单,我认为它会是,但由于某种原因,我无法正确地将它传递给 function。

To save time I have created a jsFiddle - http://jsfiddle.net/wgrills/hKxgU/4/为了节省时间,我创建了一个 jsFiddle - http://jsfiddle.net/wgrills/hKxgU/4/

Edit: Sorry to be poor at posting the question.编辑:很抱歉发布这个问题很糟糕。

I missed out most of the items because I wanted to speed the jsfiddle up.我错过了大部分项目,因为我想加快 jsfiddle 的速度。 I have updated the jsFiddle - http://jsfiddle.net/wgrills/hKxgU/7/ .我已经更新了 jsFiddle - http://jsfiddle.net/wgrills/hKxgU/7/

If you click on the + or - buttons the subtotal changes, that is all good.如果您单击 + 或 - 按钮,小计会发生变化,这一切都很好。 But I can't get the #grandTotal input to update.但我无法更新#grandTotal 输入。 The problem appears to be with the:问题似乎与:

var grandTotal = 0;
$(".subtotal").each(function() {
    $(this).css("border-color","#f00");
    grandTotal += $(this).val.split("£")[1]; 
});
$("#grandTotal").val("£" + grandTotal);
alert(grandTotal);

part of the js. js的一部分。 Note the css border change and the alert is just there for me to make sure the script is working.请注意 css 边框更改,并且警报就在那里,以确保脚本正常工作。

The code is all early days, this is just a quick mock up.代码都是早期的,这只是一个快速的模型。

You gave two problems, very easy to solve!你给了两个问题,很容易解决!

You are correct that the piece above that you posted is part of the problem.您是正确的,您发布的上述文章是问题的一部分。 In particular the line:特别是这一行:

grandTotal += $(this).val.split("£")[1];

You missed the () after val, so the code WOULD have broken here, because it doesn't know what .val.您错过了 val 之后的() ,因此代码会在这里中断,因为它不知道.val是什么.val. is.是。

Also, the code you posted was after a return false;此外,您发布的代码是在return false; this effectively tells the function is has finished, don't bother doing anything after that line.这有效地告诉 function 已经完成,不要在该行之后做任何事情。

However, as you need that section of code in both functions (clicks) its worth wrapping it in a function of its own:但是,由于您在两个函数(点击)中都需要该部分代码,因此值得将其包装在自己的 function 中:

function updateGrandTotal() {
    var grandTotal = 0;
    $(".subtotal").each(function() {
        $(this).css("border-color", "#f00");
        grandTotal += parseFloat($(this).val().split("£")[1]);
    });
    $("#grandTotal").val("£" + grandTotal);
    alert(grandTotal);
}

And calling it just before you inform the function its finished:并在您通知 function 完成之前调用它:

updateGrandTotal();
return false;

See it partially working here在这里看到它部分工作

However, while this will work on the plus of an item, you have another problem, when you are minusing an item, and the box gets to zero, instead of setting £0.00 you set it to 0, hence when it try's to split on the "£" it can't.然而,虽然这适用于一个项目的加号,但你还有另一个问题,当你减去一个项目时,盒子变为零,而不是设置 £0.00 你将它设置为 0,因此当它尝试拆分时"£"它不能。 To combat this simply copy the bit where you turn your price value into a price from the plus function into the minus function:为了解决这个问题,只需将您将价格值转换为价格的位从plus function 复制到minus function:

Replace:代替:

newprice = price * x;
$('#' + update).val(x);
$('#' + update + '_subtotal').val(newprice);

With the working version:使用工作版本:

newprice = (price * x) / 100;
newprice = newprice.toFixed(2);
newprice = '£' + newprice;
$('#' + update).val(x);
$('#' + update + '_subtotal').val(newprice);

See it fully working here在这里看到它完全工作

Your problem is with the following line:您的问题在于以下行:

grandTotal += $(this).val.split("£")[1]; 

val is a function, not a property on the returned object. val是 function,而不是返回的 object 上的属性。 You want the following instead:您需要以下内容:

grandTotal += $(this).val().split("£")[1];

Also in your fiddle you have a return false;同样在你的小提琴中,你有一个return false; in the middle of your function, which lies above the code you're calling val incorrectly on.在您的 function 的中间,它位于您错误地调用val的代码上方。 Remove that return as well.也删除该返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM