Javascript下拉式表格数学计算 [英] Javascript drop-down form math calculation

查看:144
本文介绍了Javascript下拉式表格数学计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个有两个下拉菜单的页面,然后当用户按提交时,它会根据从下拉菜单中提供的数据计算价格。它需要从第一个下拉菜单选项中选择1或2,将其乘以第二个下拉菜单选项的值,乘以0.2,并在用户按提交时输出。我试图让它工作,但我不断得到NaN错误。任何帮助?

 < head> 
< script type =text / javascript>
function price(){
var sqBn = document.priceCalc.squareOrBanner;
var Amt = document.priceCalc.number;
var price = sqBn * Amt * 0.2;
document.write(price);
}
< / script>

< form name =priceCalcaction =priceCalcPage.htmlonsubmit =document.write(price())>
< div align =center>
< select name =squareOrBanner>
< option value =1> Square< / option>
< option value =2>横幅< / option>
< / select>

< select name =number>
< option value =250> 250< / option>
< option value =500> 500< / option>
< option value =750> 750< / option>
< option value =1000> 1000< / option>
< / select>
< br>< input type =submitvalue =找出定价!>< br>
< / div>
< / form>


解决方案

也许你正在寻找这样的东西: p>

http://jsfiddle.net/JSvSn/1/



您需要访问这些对象中的属性:

  var price = sqBn.value * Amt.value * 0.2; 

编辑:这是一个更新输入框的值的版本价格: http://jsfiddle.net/JSvSn/5/



编辑2: 在div中显示价格。


I'm trying to create a page where there are two dropdown menus, and then when the user presses submit, it calculates a price based on the data given from the dropdown menu. It needs to take either 1 or 2 from the first dropdown menu choice, multiply that by the value of the second dropdown menu choice, multiply that by 0.2, and output it when the user presses submit. I've tried to make it work but I keep getting NaN errors. Any help?

<head>
<script type="text/javascript">
function price() {
        var sqBn = document.priceCalc.squareOrBanner;
        var Amt = document.priceCalc.number;
    var price = sqBn * Amt * 0.2;
    document.write(price);
}
</script>

<form name="priceCalc" action="priceCalcPage.html" onsubmit="document.write(price())">
<div align="center">
<select name="squareOrBanner">
<option value="1">Square</option>
<option value="2">Banner</option>
</select>

<select name="number">
<option value="250">250</option>
<option value="500">500</option>
<option value="750">750</option>
<option value="1000">1000</option>
</select>
<br><input type="submit" value="Figure out pricing!"><br>
</div>
</form>

解决方案

Maybe you are looking for something like this:

http://jsfiddle.net/JSvSn/1/

You need to access the value property in those objects:

var price = sqBn.value * Amt.value * 0.2;

EDIT: Here's a version that updates the value of an input box with the price: http://jsfiddle.net/JSvSn/5/

EDIT 2: Show price in a div.

这篇关于Javascript下拉式表格数学计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆