文本框之间的Javascript计算

时间:2011-11-24 06:09:41

标签: javascript html

使用aa()输入字段的值调用我的Javascript函数paying_now。我想找到paying_nowpaid的值的总和,以及balancepaying_now的差异。

将从数据库中提取netpaybalancepaid的值。在这里,我将net_pay设置为1000,作为示例。

此代码仅适用于余额,并且在应添加paid值时不会更改paying_now文本框。 NaN也在发生。是什么给了什么?

的Javascript

   function aa(paying_now)
    {
        var net_pay = document.getElementById('net_pay').value;

        var paid = document.getElementById('paid_hidden').value;
        var balance = document.getElementById('balance_hidden').value;

        if(parseInt(paying_now) > parseInt(net_pay))
        {
            document.getElementById('paying_now').value=0;
            document.getElementById('paid').value=paid;
            document.getElementById('balance').value=balance;
            alert("Amount Paying now cannot be higher than Netpay");
            return;
        }
        else if(parseInt(paying_now) > parseInt(balance))
        {
            document.getElementById('paying_now').value=0;
            document.getElementById('paid').value=paid;
            document.getElementById('balance').value=balance;
            alert("Amount Paying now cannot be higher than balance amount");
            return;

        }

    document.getElementById('paid').value = parseInt(paid)+parseInt(paying_now);
    document.getElementById('balance').value = parseInt(balance)-parseInt(paying_now);

    }

HTML

<table> 
    <tr>
        <td>
            <strong>Netpay:</strong>
            <input type='text' id='net_pay' value = '1000' readonly />
        </td>
        <td>
            Paid: <input type='text' value='100' id='paid' name='paid' readonly />
            <input type='hidden' value='100' id='paid_hidden' ></td>
        </td>
        <td>
            <strong>Balance:</strong>
            <input type='text' value='900' id='balance' readonly />
            <input type='hidden' value='900' id='balance_hidden' />
        </td>

        <td>
            <strong>Paying now:</strong><input type='text' id='paying_now' onkeyUp='aa(this.value);' />
        </td>
    </tr>
</table>

对于它的价值,这里有一个带有此代码的JSFiddle:http://jsfiddle.net/JeSZX/

4 个答案:

答案 0 :(得分:3)

您的JScript代码基本上完成了您所描述的操作(但我可能已经理解错了)。

只要您在现在支付的字段中有一些不是数字(p.e. nothing或字母)的值,就会出现NaN。为了解决这个问题,您需要先检查变量paying_now的内容,然后再对其进行算术运算,p.e。使用isNaN()函数:

if (isNaN(paying_now) == false) {
    alert ("Not a number!");
    return;
}

您的演示HTML代码稍微模糊了问题,回声和表格构造不是您问题的一部分。

尝试稍微简单一点,如下所示:

Netpay:
<input type='text' id='net_pay' value = '1000' readonly />
Paid:
<input type='text' value='100' id='paid' name='paid' readonly />
<input type='hidden' value='100' id='paid_hidden' />
Balance:
<input type='text' value='900' id='balance' readonly />
<input type='hidden' value='900' id='balance_hidden' />
Paying now:
<input type='text' id='paying_now' onkeyUp='aa(this.value);' />

答案 1 :(得分:2)

我修改了代码。请执行此操作,其工作正常。

function aa(paying_now)
{
    var net_pay = document.getElementById('net_pay').value;
    var paid = document.getElementById('paid_hidden').value;
    var balance = document.getElementById('balance_hidden').value;

    if(paying_now === ""){
        document.getElementById('paid').value = paid;
        document.getElementById('balance').value = balance;
    }    
    else if(parseInt(paying_now) > parseInt(net_pay))
    {
        alert("Amount Paying now cannot be higher than Netpay");
        document.getElementById('paying_now').value="";
        document.getElementById('paid').value=paid;
        document.getElementById('balance').value=balance;
        document.getElementById('paying_now').focus();
    }
    else if(parseInt(paying_now) > parseInt(balance))
    {
        alert("Amount Paying now cannot be higher than balance amount");
        document.getElementById('paying_now').value="";
        document.getElementById('paying_now').focus();
    }
    else{
    document.getElementById('paid').value = parseInt(paid)+parseInt(paying_now);
    document.getElementById('balance').value = parseInt(balance)-parseInt(paying_now);
    }
}

html代码中有一些小错误,请改变它。例如,在某些地方,关闭标签丢失了。将隐藏字段仅放在<td></td>内。

答案 2 :(得分:2)

<html>

<head>
<script>
function aa(paying_now)
    {

    if(isNaN(parseInt(paying_now)))
{paying_now=0;
}
    var net_pay = document.getElementById('net_pay').value;

        var paid = document.getElementById('paid_hidden').value;
        var balance = document.getElementById('balance_hidden').value;

        if(parseInt(paying_now) > parseInt(net_pay))
        {
            document.getElementById('paying_now').value=0;
            document.getElementById('paid').value=paid;
            document.getElementById('balance').value=balance;
            alert("Amount Paying now cannot be higher than Netpay");
            return;
        }
        else if(parseInt(paying_now) > parseInt(balance))
        {
            document.getElementById('paying_now').value=0;
            document.getElementById('paid').value=paid;
            document.getElementById('balance').value=balance;
            alert("Amount Paying now cannot be higher than balance amount");
            return;

        }

    document.getElementById('paid').value = parseInt(paid)+parseInt(paying_now);
    document.getElementById('balance').value = parseInt(balance)-parseInt(paying_now);

    }
</script>

</head>
<body>
<form name="form1">

<table>
<tr><td>
<strong>
Netpay:<strong>
<input type='text' id='net_pay' value = '1000' readonly />
<td>Paid:<input type='text' value='100' id='paid' name='paid' readonly />
</td>
<input type='hidden' value='100' id='paid_hidden' >
<td><strong>Balance:</strong><input type='text' value='900' id='balance' readonly /></td>
<input type='hidden' value='900' id='balance_hidden' />
<td><strong>Paying now:</strong><input type='text' id='paying_now' onkeyUp='aa(this.value);' /></td></tr>

  </table>

</form>    
</body>
</html>

看看这对我的工作正常,我已经为你的代码添加了以下一行,用于处理NaN,如“”或字母

 if(isNaN(parseInt(paying_now)))
{paying_now=0;
}

答案 3 :(得分:1)

如果您只想获得付费的当前更改值,则只需删除“付费”和“付费退款”,这样您的代码就会如此。

<script>
function aa(paying_now)
{


    var net_pay = document.getElementById('net_pay').value;
    var pay_now=document.getElementById('paying_now').value;
    var paid = document.getElementById('paid_hidden').value;
    var balance = document.getElementById('balance_hidden').value;

    if(pay_now==" ")
    {
        alert("value requrie");
        document.getElementById('paying_now').focus();
        document.getElementById('paid_hidden').value=paid;
        document.getElementById('balance_hidden').value=balance;
        return;
    }

    if(parseInt(paying_now) > parseInt(net_pay))
    {
        document.getElementById('paying_now').value=0;
        document.getElementById('paid').value=paid;
        document.getElementById('balance').value=balance;
        alert("Amount Paying now cannot be higher than Netpay");
        return;
    }
    else if(parseInt(paying_now) > parseInt(balance))
    {
        document.getElementById('paying_now').value=0;
        document.getElementById('paid').value=paid;
        document.getElementById('balance').value=balance;
        alert("Amount Paying now cannot be higher than balance amount");
        return;

    }

document.getElementById('paid').value = parseInt(paying_now);
document.getElementById('balance').value = parseInt(balance)-parseInt(paying_now);

}
</script>