0

我正在尝试使用 html 表单来接受我的用户输入,然后在 javascript 上进行计算,它只显示没有计算的对话框,可能是什么问题?我不知道该怎么做,这是我到目前为止所尝试的。

Javascript

<script type="text/javascript">
function Air() {
    input = document.getElementById('Enter').value;
    TotalPurchasingAmountOrange = 0.87 * input;
    discountOrange = 0.13 * input;
    TotalPurchasingAmountMascom = 0.85 * input;
    discountMascom = 0.15 * input;
    TotalPurchasingAmountBMobile = 0.83 * input;
    discountMascom = 0.17 * input;
    alert("Orange airtime:\nAmount: " + TotalPurchasingAmountOrange + "\nDiscount: " + discountOrange);
    alert("Mascom airtime:\nAmount: " + TotalPurchasingAmountMascom + "\nDiscount: " + discountMascom + "\nBMobile airtime:\nAmount: " + TotalPurchasingAmountBMobile + "\nDiscount: " + discountBMobile);
}
</script>

形式

<ul>
    <li>
        <label>Units
            <input type="text" data-bind="value: units" />
        </label>
    </li>
</ul>
<div class="buttonArea">
    <input id="Enter" type="button" value="Enter" onclick="Air()" />
</div>
4

3 回答 3

1

你的问题在这里:

input = document.getElementById('Enter').value;
...
<input id="Enter" type="button" value="Enter" onclick="Air()"/>

Enter具有“Enter”的值,因此当您尝试将其乘以 时0.87,我怀疑您遇到控制台错误或 javascript 试图变得聪明并找出该值并且完全错误。

这就是你想要的:

<input type="text" id="Enter">
<input type="button" value="Enter" onclick="Air()"/>
于 2013-08-21T20:42:29.900 回答
0
input = document.getElementById('Enter').value;

首先,请在初始化变量之前使用“var”,除非“input”是全局变量。其次,根据您的代码,输入值将是“Enter”。之后,您尝试将其乘以一些数字。甚至 JS 也无法提供这种计算。

你需要使用

<input type="text" id="Enter" />

允许用户编写自己的值。

或者您可以通过提示功能使用对话窗口。它看起来像:

function Air (){
    while (isNaN(amount)) {
        var input = prompt("Please, enter needed amount. Notice, that it should be number");
        var amount = parseFloat(input);
    }

    var TotalPurchasingAmountOrange = 0.87 * amount;
    var discountOrange = 0.13 * amount;

    var TotalPurchasingAmountMascom = 0.85 * amount;
    var discountMascom = 0.15 * amount;

    var TotalPurchasingAmountBMobile = 0.83 * amount;
    var discountMascom = 0.17 * amount;

    alert("Orange airtime:\nAmount: " +TotalPurchasingAmountOrange+ "\nDiscount: " +discountOrange );

    alert("Mascom airtime:\nAmount: " +TotalPurchasingAmountMascom+ "\nDiscount: " +discountMascom+ "\nBMobile airtime:\nAmount: " +TotalPurchasingAmountBMobile+ "\nDiscount: " +discountBMobile  );
}
于 2013-08-21T20:42:10.437 回答
0

Enter您在 Javascript 中获得 的值。如果您alert(input)在分配变量后执行 an ,您会注意到它的值是Enter,即按钮上的文本。不是你想要做的,我猜!

添加到您的 HTML 中,例如:

<input id="number" type="textbox">

并更改您的第一个 Javasvript 行:

input = document.getElementById('number').value;

在文本框中输入一个值,您应该会注意到最近两次alert调用中显示的实际数字!

于 2013-08-21T20:42:17.843 回答