简单的Javascript计算器 [英] SIMPLE Javascript Calculator
问题描述
首先,我不知道Javascript我很害怕,所以我希望
有人能够给我一个现成的解决方案来解决我的
问题!
我的一个朋友(诚实!)想要在他的网站上有一个Javascript
计算器用于计算他们想要的成本,例如:
1小部件和2个widglets =£5.00
2个小部件和2个widglets =£7.00
等等
他想要一个解决方案,以便用户面对两个(或者可能
more)下拉框,他们选择了多少窗口小部件。和
" widglets"他们想购买,脚本输出价格(通过
乘以这两个数字)。
可以这样做吗?如果是这样,有人可以请给我
脚本。
TIA,Neil。
Hi,
Firstly, I know NOTHING about Javascript I''m afraid, so I''m hoping that
someone will be able to point me to a ready-made solution to my
problem!
A friend of mine (honest!) is wanting to have on his site, a Javascript
Calculator for working out the cost of what they want, for example:
1 widget and 2 widglets = £5.00
2 widgets and 2 widglets = £7.00
etc etc
He is wanting a solution so that users are faced with two (or maybe
more) drop down boxes where they select how many "widgets" and
"widglets" they want to buy, and the script outputs the price (by
multiplying the two figures).
Can this be done? If so, could someone please kindly supply me with the
script.
TIA, Neil.
推荐答案
fi ******** ***?gmail.com D?D ??? D°D?(D°):
fi***********@gmail.com D?D???D°D?(D°):
Hi,
首先,我知道关于Javascript的事我很害怕,所以我希望
有人可以指点我我的
问题的现成解决方案!
Firstly, I know NOTHING about Javascript I''m afraid, so I''m hoping that
someone will be able to point me to a ready-made solution to my
problem!
我的一个朋友(诚实!)想要在他的网站上有一个Javascript
计算器用于锻炼他们想要的成本,例如:
A friend of mine (honest!) is wanting to have on his site, a Javascript
Calculator for working out the cost of what they want, for example:
1 widget和2 widglets =?£5.00
2个小部件和2 widglets =?£7.00
1 widget and 2 widglets = ?£5.00
2 widgets and 2 widglets = ?£7.00
etc etc
etc etc
他是想要一个解决方案,以便用户面对两个(或者更多的b
以下)下拉框,他们选择了多少窗口小部件。和
" widglets"他们想买,脚本输出价格(按两个数字乘以
)。
He is wanting a solution so that users are faced with two (or maybe
more) drop down boxes where they select how many "widgets" and
"widglets" they want to buy, and the script outputs the price (by
multiplying the two figures).
可以这样做吗?如果是这样,有人可以请给我
脚本。
Can this be done? If so, could someone please kindly supply me with the
script.
TIA,Neil。
TIA, Neil.
------------------------------
你可以试试这个:
< html>
< head>
< style type =" text / css"><! -
input.num {font-family:monospace; text-align:Right}
h1 {font-family:Comic Sans MS; font-size:16pt;颜色:#008080;
font-weight:bold;边距:0像素;填充:0px}
// - >< / style>
< title>交互式JavaScript订单表格< / title>
< script language =" javascript"><! -
var RowsInForm = 5 //订单中的订单项数量
表格?
var ProductsInList = 10 //产品清单中有多少产品?
var SalesTaxRate = 0.0600 //设置为十进制的销售税率。例如
0.0775是6.00%。
var TaxableState =" FL" //设置为您收取销售税的州名称
in。
var ProdSubscript = 0 //标识所选产品的下标
当前行。
函数MakeArray(n){
this.length = n
for(var i = 1; i< = n; i ++){this [i] = 0}
返回此
}
函数BuildZeroArray(n){
this.length = n
for(var i = 0; i< = n; i ++){this [i] = 0}
返回此
}
函数prodobj(名称,单价){
this.name = name
this.unitprice = unitprice
}
函数ordobj(prodsub,qty,unitprice,extprice){
this .prodsub = prodsub
this.qty = qty
this.unitprice = unitprice
this.extprice = extprice
}
函数updateRow(rownum){
var
exeLine =''ProdSubscript = document.ordform.prodchosen'' + ROWNUM + '' 的selectedIndex
eval(exeLine)
ordData [rownum] .prodsub = ProdSubscript
var exeLine =''tempqty = document.ordform.qty'' + rownum +''。va lue''
eval(exeLine)
ordData [rownum] .qty = tempqty-0 // - 从
产品价格表。
ordData [rownum] .unitprice = prodlist [ProdSubscript] .unitprice
ordData [rownum] .extprice = (ordData [rownum] .qty)* ordData [rownum] .unitprice
var
exeLine =''document.ordform.unitprice''+ rownum +''。valu e = currency(ordData [''+ rownum +'']。unitprice,10)''
eval(exeLine)
var
exeLine = ''document.ordform.extprice''+ rownum +''。value = currency(ordData [''+ rownum +'']。extprice,10)''
eval(exeLine)
updateTotals()
}
函数updateTotals(){
var subtotal = 0
for(var i = 1; i< = RowsInForm; i ++){
subtotal = subto tal + ordData [i] .extprice
}
document.ordform.subtotal.value =货币(小计,10)
salestax = 0
if(document.ordform.Taxable.checked){
salestax = SalesTaxRate *小计
}
文件.ordform.salestax.value = currency(salestax,10)
document.ordform.grandtotal.value = currency(小计+ salestax,10)
}
函数copyAddress(){
document.ordform.shipName.value = document.ordform.b illName.value
文件.ordform.shipCompany.value = document.ordfor m.billCompany.value
document.ordform.shipAdd1.value = document.ordform.b illAdd1.value
document.ordform .shipAdd2.value = document.ordform.b illAdd2.value
document.ordform.shipCSZ.value = document.ordform.bi llCSZ.value
document.ordform.shipPhone .value的= document.ordform。 billPhone.value
document.ordform.shipEmail.value = document.ordform。 billEmail.value
}
函数货币(anynum,width){
anynum = eval(anynum)
workNum = Math.abs((Math.round(anynum * 100)/ 100)); workStr ="" + workNum
if(workStr.indexOf("。) ;)== - 1){workStr + =" .00"}
dStr = workStr.substr(0,workStr.indexOf("。")); dNum = d Str-0
pStr = workStr.substr(workStr.indexOf("。"))
while(pStr.length< 3){pStr + =" 0"}
if(dNum> = 1000){
dLen = dStr.length
dStr = parseInt("" +( dNum / 1000))+"," + dStr.substring(dLen-3,dLen)
}
if(dNum> = 1000000) {
dLen = dStr.length
dStr = parseInt("" +(dNum / 1000000))+"," + dStr.substring(dLen- 7,dLen)
}
retval = dStr + pStr
if(anynum< 0){
retval = retval.substring(1,retval.length)
retval ="(" + retval +")"
}
retval ="
------------------------------
Hi,
you can try this:
<html>
<head>
<style type="text/css"><!--
input.num { font-family: monospace; text-align: Right }
h1 {font-family: Comic Sans MS; font-size: 16pt; color: #008080;
font-weight: bold; margin-bottom:0px; padding:0px}
//--></style>
<title>Interactive JavaScript Order Form</title>
<script language="javascript"><!--
var RowsInForm = 5 //How many line items will be in the order
form?
var ProductsInList = 10 //How many products in your product list?
var SalesTaxRate = 0.0600 //Set to sales tax rate in decimal. e.g.
0.0775 is 6.00%.
var TaxableState = "FL" //Set to name of state you charge sales tax
in.
var ProdSubscript = 0 //Identifies subscript of selected product
in current row.
function MakeArray(n) {
this.length = n
for (var i=1;i<=n;i++) {this[i]=0}
return this
}
function BuildZeroArray(n) {
this.length = n
for (var i=0;i<=n;i++) {this[i]=0}
return this
}
function prodobj(name, unitprice) {
this.name = name
this.unitprice = unitprice
}
function ordobj(prodsub, qty, unitprice, extprice) {
this.prodsub = prodsub
this.qty = qty
this.unitprice = unitprice
this.extprice = extprice
}
function updateRow(rownum){
var
exeLine=''ProdSubscript=document.ordform.prodchosen ''+rownum+''.selectedIndex''
eval(exeLine)
ordData[rownum].prodsub=ProdSubscript
var exeLine=''tempqty=document.ordform.qty''+rownum+''.va lue''
eval(exeLine)
ordData[rownum].qty=tempqty-0 //-- Gets unit price from the
product price list.
ordData[rownum].unitprice=prodlist[ProdSubscript].unitprice
ordData[rownum].extprice=(ordData[rownum].qty)*ordData[rownum].unitprice
var
exeLine=''document.ordform.unitprice''+rownum+''.valu e=currency(ordData[''+rownum+''].unitprice,10)''
eval (exeLine)
var
exeLine=''document.ordform.extprice''+rownum+''.value =currency(ordData[''+rownum+''].extprice,10)''
eval(exeLine)
updateTotals()
}
function updateTotals() {
var subtotal = 0
for (var i=1;i<=RowsInForm;i++) {
subtotal=subtotal+ordData[i].extprice
}
document.ordform.subtotal.value = currency(subtotal,10)
salestax=0
if (document.ordform.Taxable.checked) {
salestax = SalesTaxRate*subtotal
}
document.ordform.salestax.value = currency(salestax,10)
document.ordform.grandtotal.value = currency(subtotal+salestax,10)
}
function copyAddress() {
document.ordform.shipName.value=document.ordform.b illName.value
document.ordform.shipCompany.value=document.ordfor m.billCompany.value
document.ordform.shipAdd1.value=document.ordform.b illAdd1.value
document.ordform.shipAdd2.value=document.ordform.b illAdd2.value
document.ordform.shipCSZ.value=document.ordform.bi llCSZ.value
document.ordform.shipPhone.value=document.ordform. billPhone.value
document.ordform.shipEmail.value=document.ordform. billEmail.value
}
function currency(anynum,width) {
anynum=eval(anynum)
workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
if (workStr.indexOf(".")==-1){workStr+=".00"}
dStr=workStr.substr(0,workStr.indexOf("."));dNum=d Str-0
pStr=workStr.substr(workStr.indexOf("."))
while (pStr.length<3){pStr+="0"}
if (dNum>=1000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
}
if (dNum>=1000000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
}
retval=dStr+pStr
if (anynum < 0) {
retval=retval.substring(1,retval.length)
retval="("+retval+")"
}
retval = "
" + retval
// - 使用前导空格填充以更好地对齐数字。
while(retval.length< width){retval =" + retval}
返回retval
}
// - >< / script>
< / head>
< body>
< CENTER>< h2>交互式订单表格< / h2>< / CENTER>
< BLOCKQUOTE>< BLOCKQUOTE>< BLOCKQUOTE>
< p>这是一个假设的订单表格,它使用了相当多的高级>
JavaScript代码。要试用它,从下拉列表中选择一个产品,
然后输入数量并单击另一个字段,或按Tab键。还有
还有一个按钮,用于将信息从账单复制到表格的一部分到
船到部分表格。
< br>< / BLOCKQUOTE>< / BLOCKQUOTE>< / BLOCKQUOTE>< BR>
< script language =" JavaScript"><! -
prodlist = new BuildZeroArray(ProductsInList)
prodlist [0] = new prodobj('' - none - '',0)
prodlist [1] = new prodobj(''Bumper Sticker'',10.00)
prodlist [2] = new prodobj(''Lapel Pin'',10.50)>
prodlist [3] = new prodobj(''Ball Cap'',11.00)
prodlist [4] = new prodobj(''Calendar'',11.99)
prodlist [5] = new prodobj(''Braided Ball Cap'',12.00)
prodlist [6] = new prodobj(''License Plate'',14.99)
prodlist [7] = new prodobj(''One Year Membership'',25.00)
prodlist [8] = new prodobj(''Wrist Watch'',99.99)
prodlist [9] = n ew prodobj(''五年会员'',100.00)
prodlist [10] =新prodobj(''终身会员'',250.00)
ordData = new MakeArray(RowsInForm)
for(var i = 1;我< = RowsInForm; i ++){
ordData [i] = new ordobj(0,0,0,0)
}
// - > < / script>
< / p>
< form name =" ordform"方法= QUOT; POST" action =" someHandler">
< table align =" center"边界=" 1 QUOT; bgcolor ="#800000">< tr>
< th width =" 192" BGCOLOR =" YELLOW">< b> Product< / b>< / th>
< th width =" 72" BGCOLOR = QUOT; YELLOW" align =" center">< b> Qty< / b>< / th>
< th width =" 120" BGCOLOR = QUOT; YELLOW" align =" center">< b>单价< / b>< / th>
< th width =" 120" BGCOLOR = QUOT; YELLOW" align =" center">< b>分机价格< / b>< / th>
< / tr>
< script language =" JavaScript"><! -
for(var rownum = 1; rownum< = RowsInForm; rownum ++){
document.write(' '< tr>< td width = 192 BGCOLOR =" CYAN">'')
document.write(''< select name =" prodchosen''+ rownum +''
onChange =" updateRow(''+ rownum +'')">'')
for(i = 0; i< = ProductsInList; i ++ ){
document.write("< option>" + prodlist [i] .name)
}
document.write (''< / select>< / td>'')
document.write(''< td width = 72 align =" center" BGCOLOR =" CYAN"> < input
class =" num" name =" qty''+ rownum +''" value =""'')
document.write (''size = 3 onChange =" updateRow(''+ rownum +'')">< ; / td>'')
document.write(''< td width = 120 align =" center" BGCOLOR =CYAN>'')
document.write(''< input class =" num" name =" unitprice''+ rownum +''"
value =""'')
document.write(''size = 10 onfocus =" this.blur()">< / td>' ')
document.write(''< td width = 120 align =" center" BGCOLOR =" CYAN">'')
document.write (''< input class =" num" name =" extprice''+ rownum +''"
value =""'')
document.write(''size = 10 onfocus =" this.blur()">< / td>'')
document.write(''< / tr>' ')
}
// - >< / script>
< tr>
< td width =" 384"列跨度= QUOT; 3英寸align =" right"
BGCOLOR =" LIMEGREEN">小计:< / td>
< td width =" 120"对齐= QUOT;中心" BGCOLOR =" LIMEGREEN">< input class =" num"
name =" subtotal"大小= QUOT; 10" onfocus =" this.blur()">< / td>< / tr>
< tr>< td width =" 264"列跨度= QUOT; 2英寸BGCOLOR =" CYAN">< input type ="复选框"
name =" Taxable"值= QUOT;真" onclick =" updateTotals()">点击此处如果你
住在< script> document.write(TaxableState)< / script>< / td>
< td width =" 120"对齐= QUOT;右" BGCOLOR =" LIMEGREEN>销售税:?*< / td>
< td width =" 120"对齐= QUOT;中心" BGCOLOR =" CYAN">< input class =" num"
name =" salestax"大小= QUOT; 10" onfocus =" this.blur()">< / td>
< / tr>
< tr>
< td width =" 384"列跨度= QUOT; 3英寸对齐= QUOT;右" BGCOLOR =" YELLOW" Grand
总计:< / td>
< td width =" 120"对齐= QUOT;中心" BGCOLOR ="#800000">< input class =" num"
name =" grandtotal"大小= QUOT; 10" onfocus =" this.blur()">< / td>< / tr>< / table>
< / body>
< / html>
问候,
Mistral
"+retval
//--Pad with leading blanks to better align numbers.
while (retval.length<width){retval=" "+retval}
return retval
}
//--></script>
</head>
<body>
<CENTER><h2>Interactive Order Form</h2></CENTER>
<BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE>
<p>Here''s a hypothetical order form, which uses quite a bit of advanced
JavaScript code. To try it out, pick a product from the drop-down list,
then type in a quantity and click another field, or press Tab. There''s
also a button to copy information from the Bill To part of the form to
the Ship To part of the form.
<br></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE><BR>
<script language="JavaScript"><!--
prodlist = new BuildZeroArray(ProductsInList)
prodlist[0] = new prodobj(''-none-'',0)
prodlist[1] = new prodobj(''Bumper Sticker'',10.00)
prodlist[2] = new prodobj(''Lapel Pin'',10.50)
prodlist[3] = new prodobj(''Ball Cap'',11.00)
prodlist[4] = new prodobj(''Calendar'',11.99)
prodlist[5] = new prodobj(''Braided Ball Cap'',12.00)
prodlist[6] = new prodobj(''License Plate'',14.99)
prodlist[7] = new prodobj(''One Year Membership'',25.00)
prodlist[8] = new prodobj(''Wrist Watch'',99.99)
prodlist[9] = new prodobj(''Five Year Membership'',100.00)
prodlist[10] = new prodobj(''Life Time Membership'',250.00)
ordData = new MakeArray(RowsInForm)
for (var i=1; i<= RowsInForm; i++) {
ordData[i] = new ordobj(0,0,0,0)
}
//--></script>
</p>
<form name="ordform" method="POST" action="someHandler">
<table align="center" border="1" bgcolor="#800000"><tr>
<th width="192" BGCOLOR="YELLOW"><b>Product</b></th>
<th width="72" BGCOLOR="YELLOW" align="center"><b>Qty</b></th>
<th width="120" BGCOLOR="YELLOW" align="center"><b>Unit Price</b></th>
<th width="120" BGCOLOR="YELLOW" align="center"><b>Ext Price</b></th>
</tr>
<script language="JavaScript"><!--
for (var rownum=1;rownum<=RowsInForm;rownum++) {
document.write(''<tr><td width=192 BGCOLOR="CYAN">'')
document.write(''<select name="prodchosen''+rownum+''"
onChange="updateRow(''+rownum+'')">'')
for (i=0; i<=ProductsInList; i++) {
document.write ("<option>"+prodlist[i].name)
}
document.write (''</select></td>'')
document.write (''<td width=72 align="center" BGCOLOR="CYAN"><input
class="num" name="qty''+rownum+''" value=""'')
document.write (''size=3 onChange="updateRow(''+rownum+'')"></td>'')
document.write (''<td width=120 align="center" BGCOLOR="CYAN">'')
document.write (''<input class="num" name="unitprice''+rownum+''"
value="" '')
document.write (''size=10 onfocus="this.blur()"></td>'')
document.write (''<td width=120 align="center" BGCOLOR="CYAN">'')
document.write (''<input class="num" name="extprice''+rownum+''"
value="" '')
document.write (''size=10 onfocus = "this.blur()"></td>'')
document.write (''</tr>'')
}
//--></script>
<tr>
<td width="384" colspan="3" align="right"
BGCOLOR="LIMEGREEN">Subtotal:</td>
<td width="120" align="center" BGCOLOR="LIMEGREEN"><input class="num"
name="subtotal" size="10" onfocus="this.blur()"></td></tr>
<tr><td width="264" colspan="2" BGCOLOR="CYAN"><input type="checkbox"
name="Taxable" value="true" onclick="updateTotals()">Click here if you
live in <script>document.write(TaxableState)</script></td>
<td width="120" align="right" BGCOLOR="LIMEGREEN">Sales Tax:?*</td>
<td width="120" align="center" BGCOLOR="CYAN"><input class="num"
name="salestax" size="10" onfocus="this.blur()"></td>
</tr>
<tr>
<td width="384" colspan="3" align="right" BGCOLOR="YELLOW">Grand
Total:</td>
<td width="120" align="center" BGCOLOR="#800000"><input class="num"
name="grandtotal" size="10" onfocus="this.blur()"></td></tr></table>
</body>
</html>
Regards,
Mistral
这看起来有点复杂的我的需要。如果我只有两次下降
(A& B),我该怎么做: -
将B乘以2
添加A
添加常量(C),然后将结果输出到一个小的只读
文本框中。如果需要,可以从只读更改。
问候
Richard
mistral写道:
This looks a little complexfor my needs. If I only have two drop downs
(A & B), how would I do the following:-
Multiply B by 2
Add on A
add on a constants (C) and then output the result to a small read-only
text-box. It can be changed from read-only if necessary.
Regards
Richard
mistral wrote:
fi ********** *@gmail.com D?D ??? D°D?(D°):
fi***********@gmail.com D?D???D°D?(D°):
您好,
Hi,
首先,我知道没什么关于Javascript的我很害怕,所以我希望
有人可以指点我做好准备我的解决方案我的
问题!
Firstly, I know NOTHING about Javascript I''m afraid, so I''m hoping that
someone will be able to point me to a ready-made solution to my
problem!
我的一个朋友(诚实!)想要在他的网站上有一个Javascript
计算器用于锻炼他们想要的成本,例如:
A friend of mine (honest!) is wanting to have on his site, a Javascript
Calculator for working out the cost of what they want, for example:
1 widget和2 widglets =?£5.00
2个小部件和2 widglets =?£7.00
1 widget and 2 widglets = ?£5.00
2 widgets and 2 widglets = ?£7.00
etc etc
etc etc
他是想要一个解决方案,以便用户面对两个(或者更多的b
以下)下拉框,他们选择了多少窗口小部件。和
" widglets"他们想买,脚本输出价格(按两个数字乘以
)。
He is wanting a solution so that users are faced with two (or maybe
more) drop down boxes where they select how many "widgets" and
"widglets" they want to buy, and the script outputs the price (by
multiplying the two figures).
可以这样做吗?如果是这样,有人可以请给我
脚本。
Can this be done? If so, could someone please kindly supply me with the
script.
TIA,Neil。
TIA, Neil.
------------------------------
你可以试试这个:
< html>
< head>
< style type =" text / css"><! -
input.num {font-family:monospace; text-align:Right}
h1 {font-family:Comic Sans MS; font-size:16pt;颜色:#008080;
font-weight:bold;边距:0像素;填充:0px}
// - >< / style>
< title>交互式JavaScript订单表格< / title>
< script language =" javascript"><! -
var RowsInForm = 5 //订单中的订单项数量
表格?
var ProductsInList = 10 //产品清单中有多少产品?
var SalesTaxRate = 0.0600 //设置为十进制的销售税率。例如
0.0775是6.00%。
var TaxableState =" FL" //设置为您收取销售税的州名称
in。
var ProdSubscript = 0 //标识所选产品的下标
当前行。
函数MakeArray(n){
this.length = n
for(var i = 1; i< = n; i ++){this [i] = 0}
返回此
}
函数BuildZeroArray(n){
this.length = n
for(var i = 0; i< = n; i ++){this [i] = 0}
返回此
}
函数prodobj(名称,单价){
this.name = name
this.unitprice = unitprice
}
函数ordobj(prodsub,qty,unitprice,extprice){
this .prodsub = prodsub
this.qty = qty
this.unitprice = unitprice
this.extprice = extprice
}
函数updateRow(rownum){
var
exeLine =''ProdSubscript = document.ordform.prodchosen' + rownum +''。selectedIndex''
eval(exeLine)
ordData [rownum] .prodsub = ProdSubscript
var exeLine =''tempqty = document.ordform.qty''+ rownum +''。va lue''
eval(exeLine)
ordData [rownum] .qty = tempqty-0 // - - 从
产品价格表中获取单价。
ordData [rownum] .unitprice = prodlist [ProdSubscript] .unitprice
ordData [rownum] .extprice =(ordData [rownum] .qty)* ordData [rownum] .unitprice
var
exeLine =''document.ordform.unitprice' '+ rownum +''。评估e =货币(ordData [''+ rownum +'']。unitprice,10)''
eval(exeLine)
var
exeLine =''document.ordform.extprice''+ rownum +''。value = currency(ordData [''+ rownum +'']。extprice,10)''
eval(exeLine)
updateTotals()
}
函数updateTotals(){
var小计= 0
for(var i = 1; i< = RowsInForm; i ++){
subtotal = subtotal + ordData [i] .extprice
}
document.ordform.subtotal.value = currency(小计,10)
salestax = 0
if(document.ordform.Taxable.checked){
salestax = SalesTaxRate *小计
}
document.ordform.salestax.value = currency(salestax,10)
document.ordform.grandtotal.value = currency(小计+ salestax,10)
}
函数copyAddress(){
document.ordform.shipName.value = document.ordform.b illName.value
document.ordform.shipCompany.value = document.ordfor m.billCompany.value
document.ordform.shipAdd1.value = document.ordform.b illAdd1.value
document.ordform.shipAdd2.value = document.ordform.b illAdd2.value
document.ordform.shipCSZ.value = document.ordform.bi llCSZ.value
document.ordform.shipPhone.value = document .ordform。 billPhone.value
document.ordform.shipEmail.value = document.ordform。 billEmail.value
}
函数货币(anynum,width){
anynum = eval(anynum)
workNum = Math.abs((Math.round(anynum * 100)/ 100)); workStr ="" + workNum
if(workStr.indexOf("。) ;)== - 1){workStr + =" .00"}
dStr = workStr.substr(0,workStr.indexOf("。")); dNum = d Str-0
pStr = workStr.substr(workStr.indexOf("。"))
while(pStr.length< 3){pStr + =" 0"}
if(dNum> = 1000){
dLen = dStr.length
dStr = parseInt("" +( dNum / 1000))+"," + dStr.substring(dLen-3,dLen)
}
if(dNum> = 1000000) {
dLen = dStr.length
dStr = parseInt("" +(dNum / 1000000))+"," + dStr.substring(dLen- 7,dLen)
}
retval = dStr + pStr
if(anynum< 0){
RETVAL = retval.substring(1,retval.length )
retval ="(" + retval +")"
}
retval ="
------------------------------
Hi,
you can try this:
<html>
<head>
<style type="text/css"><!--
input.num { font-family: monospace; text-align: Right }
h1 {font-family: Comic Sans MS; font-size: 16pt; color: #008080;
font-weight: bold; margin-bottom:0px; padding:0px}
//--></style>
<title>Interactive JavaScript Order Form</title>
<script language="javascript"><!--
var RowsInForm = 5 //How many line items will be in the order
form?
var ProductsInList = 10 //How many products in your product list?
var SalesTaxRate = 0.0600 //Set to sales tax rate in decimal. e.g.
0.0775 is 6.00%.
var TaxableState = "FL" //Set to name of state you charge sales tax
in.
var ProdSubscript = 0 //Identifies subscript of selected product
in current row.
function MakeArray(n) {
this.length = n
for (var i=1;i<=n;i++) {this[i]=0}
return this
}
function BuildZeroArray(n) {
this.length = n
for (var i=0;i<=n;i++) {this[i]=0}
return this
}
function prodobj(name, unitprice) {
this.name = name
this.unitprice = unitprice
}
function ordobj(prodsub, qty, unitprice, extprice) {
this.prodsub = prodsub
this.qty = qty
this.unitprice = unitprice
this.extprice = extprice
}
function updateRow(rownum){
var
exeLine=''ProdSubscript=document.ordform.prodchosen ''+rownum+''.selectedIndex''
eval(exeLine)
ordData[rownum].prodsub=ProdSubscript
var exeLine=''tempqty=document.ordform.qty''+rownum+''.va lue''
eval(exeLine)
ordData[rownum].qty=tempqty-0 //-- Gets unit price from the
product price list.
ordData[rownum].unitprice=prodlist[ProdSubscript].unitprice
ordData[rownum].extprice=(ordData[rownum].qty)*ordData[rownum].unitprice
var
exeLine=''document.ordform.unitprice''+rownum+''.valu e=currency(ordData[''+rownum+''].unitprice,10)''
eval (exeLine)
var
exeLine=''document.ordform.extprice''+rownum+''.value =currency(ordData[''+rownum+''].extprice,10)''
eval(exeLine)
updateTotals()
}
function updateTotals() {
var subtotal = 0
for (var i=1;i<=RowsInForm;i++) {
subtotal=subtotal+ordData[i].extprice
}
document.ordform.subtotal.value = currency(subtotal,10)
salestax=0
if (document.ordform.Taxable.checked) {
salestax = SalesTaxRate*subtotal
}
document.ordform.salestax.value = currency(salestax,10)
document.ordform.grandtotal.value = currency(subtotal+salestax,10)
}
function copyAddress() {
document.ordform.shipName.value=document.ordform.b illName.value
document.ordform.shipCompany.value=document.ordfor m.billCompany.value
document.ordform.shipAdd1.value=document.ordform.b illAdd1.value
document.ordform.shipAdd2.value=document.ordform.b illAdd2.value
document.ordform.shipCSZ.value=document.ordform.bi llCSZ.value
document.ordform.shipPhone.value=document.ordform. billPhone.value
document.ordform.shipEmail.value=document.ordform. billEmail.value
}
function currency(anynum,width) {
anynum=eval(anynum)
workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
if (workStr.indexOf(".")==-1){workStr+=".00"}
dStr=workStr.substr(0,workStr.indexOf("."));dNum=d Str-0
pStr=workStr.substr(workStr.indexOf("."))
while (pStr.length<3){pStr+="0"}
if (dNum>=1000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
}
if (dNum>=1000000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
}
retval=dStr+pStr
if (anynum < 0) {
retval=retval.substring(1,retval.length)
retval="("+retval+")"
}
retval = "
这篇关于简单的Javascript计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!