[英]How I can create a javascript and html sales tax calculator?
Ok my problem is, I want to create a sales tax calculator. 好的,我的问题是,我想创建一个营业税计算器。 When the user, put the price of a item ( example a ball that cost 7 dollars) calculate their final price with the tax of 12%. 当用户输入物品的价格(例如,一个球的价格为7美元)时,其最终价格为12%。
But.. not just that, I want, if the user pick some item ( ball: $7, toy $4 and candy $2.. = $13) sum that item and add the sales tax ( $13 + 12%). 但是..不仅如此,我还想,如果用户选择某件商品(球:$ 7,玩具$ 4和糖果$ 2 .. = $ 13),则将该商品加起来并加营业税($ 13 + 12%)。
in my code I have the basic structure dont worry for that. 在我的代码中,我有基本的结构不必为此担心。
Below i the code that i wrote, and dont works. 下面我我写的代码,不起作用。 I use HTML and Javascript. 我使用HTML和Javascript。
Please help me. 请帮我。
function CalculateIVU() { var price = document.forms.txts.value; var evaluation = price; var tax = 0.12 * evaluation; var final = +tax + +evaluation; document.getElementById('res').value = final; }
<form name="forms" id="forma"> <table> <tr> <td> <label>enter the value</label> <input type="text" name="txts" size=20> </br> <label>the tax is</label> <input type="text" name="resul" id="res"> </td> </tr> <tr> <td> <INPUT TYPE="button" NAME="one" VALUE=" 1 " OnClick="forms.txts.value += '1'"> <INPUT TYPE="button" NAME="two" VALUE=" 2 " OnCLick="forms.txts.value += '2'"> <INPUT TYPE="button" NAME="three" VALUE=" 3 " OnClick="forms.txts.value += '3'"> <INPUT TYPE="button" NAME="plus" VALUE=" + " OnClick="forms.txts.value += '+ '"> <br> <INPUT TYPE="button" NAME="four" VALUE=" 4 " OnClick="forms.txts.value += '4'"> <INPUT TYPE="button" NAME="five" VALUE=" 5 " OnCLick="forms.txts.value += '5'"> <INPUT TYPE="button" NAME="six" VALUE=" 6 " OnClick="forms.txts.value += '6'"> <br> <INPUT TYPE="button" NAME="seven" VALUE=" 7 " OnClick="forms.txts.value+= '7'"> <INPUT TYPE="button" NAME="eight" VALUE=" 8 " OnCLick="forms.txts.value += '8'"> <INPUT TYPE="button" NAME="nine" VALUE=" 9 " OnClick="forms.txts.value += '9'"> <br> <INPUT TYPE="button" NAME="clear" VALUE=" c " OnClick="forms.txts.value = ''" OnClick="forms.resul.value = ''"> <INPUT TYPE="button" NAME="zero" VALUE=" 0 " OnClick="forms.txts.value += '0'"> <INPUT TYPE="button" NAME="DoIt" VALUE=" = " OnClick="CalculateIVU() = eval(forms.txts.value);"> <br> <input type="button" id="input" value="Calcular" onClick='CalculateIVU();'> </td> </tr> </form> </div>
Your code as shown works for a single value when pressing the Calcular button. 按下Calcular按钮时,显示的代码仅适用于单个值。 The = button doesn't work because CalculateIVU() = eval(forms.txts.value);
“ =”按钮不起作用,因为CalculateIVU() = eval(forms.txts.value);
is invalid JavaScript: you can't assign a function call equal to something. 是无效的JavaScript:您不能分配等于某项的函数调用。
Your use of eval()
is a simple way to do a mathematical calculation on a string that you know is in the format "1 + 2 + 3"
, however, this is an input field where the user can type anything , so if the user enters "alert()"
then using eval()
will actually display an alert box. 使用eval()
是对格式为"1 + 2 + 3"
的字符串进行数学计算的简单方法,但是,这是用户可以在其中输入任何内容的输入字段,因此如果用户输入"alert()"
然后使用eval()
实际上会显示一个警报框。 Even if they've tried to enter just numbers separated by plus signs they might have a typo or have forgotten to enter a +
or something, leading to a syntax error when eval()
ed. 即使他们试图输入仅用加号分隔的数字,他们也可能有错别字或忘记了输入+
或其他内容,从而在eval()
ed时导致语法错误。
You need to validate the input first. 您需要先验证输入。
One way to do that is with a regular expression . 一种方法是使用正则表达式 。 It's easy to write a simple regular expression that tests for any characters other than digits, decimal points, spaces, and plus signs, eg, /[^ \\d\\.+]/
, but that isn't enough because you might still have an invalid expression made up of valid characters, eg, "1+++2+3...4"
. 编写一个简单的正则表达式很容易,它可以测试除数字,小数点,空格和加号以外的任何字符,例如/[^ \\d\\.+]/
,但这还不够,因为您可能仍然拥有由有效字符组成的无效表达式,例如"1+++2+3...4"
。 So here's a more complicated regex that should valid the whole string reasonably well: 因此,这里有一个更复杂的正则表达式,可以合理地验证整个字符串:
/^\d{1,8}(\.\d{1,2})?( *\+ *\d{1,8}(\.\d{1,2})?)*$/
Explanation: 说明:
^ // match beginning of string
\d{1,8} // match from 1 to 8 digits
(\.\d{1,2})? // match a . followed by 1 or 2 digits,
// where the parentheses and ? make it optional
( // start a group
*\+ * // match zero or more spaces then a + then zero or more spaces
\d{1,8}(\.\d{1,2})? // as above, match 1-8 digits optionally followed
// by a . and 1-2 digits
) // end of group
* // match the last group zero or more times
$ // match the end of the string
In other words, that regex will match "1"
or "1 + 1"
or "1.2 + 2.3 + 3"
(etc.), but not "1 + a"
or "1 + + 1"
or "1..2 + 3"
. 换句话说,该正则表达式将匹配"1"
或"1 + 1"
或"1.2 + 2.3 + 3"
(等等),但不匹配"1"
"1 + a"
或"1 + + 1"
或"1..2 + 3"
。 You can use the regex .test()
method to see if a given string matches a regular expression. 您可以使用regex .test()
方法查看给定的字符串是否与正则表达式匹配。
So putting that into your function: 因此,将其放入您的函数中:
function CalculateIVU() {
var price = document.forms.txts.value;
if (!/^\d{1,8}(\.\d{1,2})?( *\+ *\d{1,8}(\.\d{1,2})?)*$/.test(price)) {
alert("Invalid input");
return false;
}
var sum = eval(price); // at this point we know price can be eval()ed
var tax = 0.12 * sum;
var final = sum + tax;
document.getElementById('res').value = final.toFixed(2); // round
}
Working demo: 工作演示:
function CalculateIVU() { var price = document.forms.txts.value; if (!/^\\d{1,8}(\\.\\d{1,2})?( *\\+ *\\d{1,8}(\\.\\d{1,2})?)*$/.test(price)) { alert("Invalid input"); return false; } var sum = eval(price); // at this point we know price can be eval()ed var tax = 0.12 * sum; var final = sum + tax; document.getElementById('res').value = final.toFixed(2); // round }
<form name="forms" id="forma"> <table> <tr> <td> <label>enter the value</label> <input type="text" name="txts" size=20> </br> <label>the tax is</label> <input type="text" name="resul" id="res"> </td> </tr> <tr> <td> <INPUT TYPE="button" NAME="one" VALUE=" 1 " OnClick="forms.txts.value += '1'"> <INPUT TYPE="button" NAME="two" VALUE=" 2 " OnCLick="forms.txts.value += '2'"> <INPUT TYPE="button" NAME="three" VALUE=" 3 " OnClick="forms.txts.value += '3'"> <INPUT TYPE="button" NAME="plus" VALUE=" + " OnClick="forms.txts.value += '+ '"> <br> <INPUT TYPE="button" NAME="four" VALUE=" 4 " OnClick="forms.txts.value += '4'"> <INPUT TYPE="button" NAME="five" VALUE=" 5 " OnCLick="forms.txts.value += '5'"> <INPUT TYPE="button" NAME="six" VALUE=" 6 " OnClick="forms.txts.value += '6'"> <br> <INPUT TYPE="button" NAME="seven" VALUE=" 7 " OnClick="forms.txts.value+= '7'"> <INPUT TYPE="button" NAME="eight" VALUE=" 8 " OnCLick="forms.txts.value += '8'"> <INPUT TYPE="button" NAME="nine" VALUE=" 9 " OnClick="forms.txts.value += '9'"> <br> <INPUT TYPE="button" NAME="clear" VALUE=" c " OnClick="forms.txts.value = ''" OnClick="forms.resul.value = ''"> <INPUT TYPE="button" NAME="zero" VALUE=" 0 " OnClick="forms.txts.value += '0'"> <INPUT TYPE="button" NAME="DoIt" VALUE=" = " OnClick="CalculateIVU();"> <br> <input type="button" id="input" value="Calcular" onClick='CalculateIVU();'> </td> </tr> </table> </form>
PS For those who object to eval()
for any purpose: PS对于出于任何目的反对eval()
的人:
var sum = price.split(/ *\+ */).reduce(function(a,v) { return +a + +v; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.