基于表单输入的实时Javascript计算

时间:2022-11-24 08:45:39

I'm trying to make a real-time calculator based on form inputs, when I use a sample div it works but I seem to be missing something when I try to print the total inside an input...

我正在尝试基于表单输入制作一个实时计算器,当我使用一个示例div它可以工作,但当我尝试在输入中打印总数时,我似乎错过了一些东西......

Working Markup Solution:

工作加价解决方案:

 <input id='first' type="text" class="form-control formBlock" name="bus_ticket"  placeholder="Bus Ticket..." required/><br />

<input id='second' type="text" class="form-control formBlock" name="plane_ticket"  placeholder="Plane Ticket..." required/><br />

<input id='third' type="text" class="form-control formBlock" name="hotel_expenses"  placeholder="Hotel Expenses..." required/><br />

 <input id='fourth' type="text" class="form-control formBlock" name="eating_expenses"  placeholder="Eating Expenses..." required/><br />

Total : <span id="total_expenses"></span>

Working Script

$('input').keyup(function(){ // run anytime the value changes


    var firstValue = parseFloat($('#first').val()); // get value of field
    var secondValue = parseFloat($('#second').val()); // convert it to a float
    var thirdValue = parseFloat($('#third').val());
    var fourthValue = parseFloat($('#fourth').val());

    $('#total_expenses').html(firstValue + secondValue + thirdValue + fourthValue); // add them and output it
});

Non-Working Markup

<input id='total_expenses' type="text" class="form-control formBlock" name="funding"  placeholder="Total Expenses..."/>

Non-Working Script

$('input').keyup(function(){ // run anytime the value changes
    var firstValue = parseFloat($('#first').val()); // get value of field
    var secondValue = parseFloat($('#second').val()); // convert it to a float
    var thirdValue = parseFloat($('#third').val());
    var fourthValue = parseFloat($('#fourth').val());
    document.getElementById('#total_expenses').value(firstValue + secondValue + thirdValue + fourthValue);
// add them and output it
});

3 个解决方案

#1


4  

Here's a working JSFiddle.

You've been confusing Javascript and jQuery codes.

你一直在混淆Javascript和jQuery代码。

// Wrong code:
document.getElementById('#total_expenses').value(sum)
//Correct code:
document.getElementById('total_expenses').value() = sum

Also, change parseFloat to Number so you don't get NaN when at least one of your input fields is blank.

此外,将parseFloat更改为Number,这样当至少有一个输入字段为空时,您不会获得NaN。

BONUS: change your <input type="text" /> to <input type="number" /> to prevent "non-numerical" inputs.

奖励:将更改为以防止“非数字”输入。

#2


1  

You can try this jquery code

你可以尝试这个jquery代码

var totalValue = firstValue + secondValue + thirdValue + fourthValue;
$('#total_expenses').val(totalValue);

#3


1  

First, when using getElementById you can omit the #. Secondly, assigning a value to an input using vanilla JS is done by simple assignment, and not a function call. So that line should look like this:

首先,使用getElementById时可以省略#。其次,使用vanilla JS为输入赋值是通过简单赋值完成的,而不是函数调用。所以该行应如下所示:

document.getElementById('total_expenses').value = firstValue + secondValue + thirdValue + fourthValue;

#1


4  

Here's a working JSFiddle.

You've been confusing Javascript and jQuery codes.

你一直在混淆Javascript和jQuery代码。

// Wrong code:
document.getElementById('#total_expenses').value(sum)
//Correct code:
document.getElementById('total_expenses').value() = sum

Also, change parseFloat to Number so you don't get NaN when at least one of your input fields is blank.

此外,将parseFloat更改为Number,这样当至少有一个输入字段为空时,您不会获得NaN。

BONUS: change your <input type="text" /> to <input type="number" /> to prevent "non-numerical" inputs.

奖励:将更改为以防止“非数字”输入。

#2


1  

You can try this jquery code

你可以尝试这个jquery代码

var totalValue = firstValue + secondValue + thirdValue + fourthValue;
$('#total_expenses').val(totalValue);

#3


1  

First, when using getElementById you can omit the #. Secondly, assigning a value to an input using vanilla JS is done by simple assignment, and not a function call. So that line should look like this:

首先,使用getElementById时可以省略#。其次,使用vanilla JS为输入赋值是通过简单赋值完成的,而不是函数调用。所以该行应如下所示:

document.getElementById('total_expenses').value = firstValue + secondValue + thirdValue + fourthValue;