Javascript 下拉表单数学计算

发布于 2024-09-10 13:31:08 字数 1020 浏览 3 评论 0原文

我正在尝试创建一个页面,其中有两个下拉菜单,然后当用户按下提交时,它会根据下拉菜单中给出的数据计算价格。它需要从第一个下拉菜单选项中获取 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="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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2024-09-17 13:31:08

也许您正在寻找这样的东西:

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

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

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

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

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

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.

溺孤伤于心 2024-09-17 13:31:08

我不确定 sqBn.value 是否适用于所有浏览器。使用起来会更安全

sqBn.options[sqBn.selectedIndex].value

,如果您仍然得到 NaN 结果,请尝试

parseInt(sqBn.options[sqBn.selectedIndex].value)

如果您想在不刷新页面的情况下进行此计算,请更改表单声明,如下所示:

<form name="priceCalc" action="priceCalcPage.html" onsubmit="document.write(price());return false;">    

这样,将执行 onsubmit,但不会向的行动。如果您想提交,您仍然可以通过 javascript (document.priceCalc.submit()) 进行提交。

I'm not sure if the sqBn.value will work on all browsers. It would be safer to use

sqBn.options[sqBn.selectedIndex].value

and if you are still getting NaN results then try

parseInt(sqBn.options[sqBn.selectedIndex].value)

If you want to do this calculation without refreshing the page change the form declaration like this:

<form name="priceCalc" action="priceCalcPage.html" onsubmit="document.write(price());return false;">    

In this way the onsubmit will be executed, but won't submit anything to the action. If you want to submit it, you can still do it from javascript (document.priceCalc.submit()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文