簡體   English   中英

文本框之間的JavaScript計算

[英]Javascript calculations between text boxes

我的Javascript函數aa()是使用paying_now輸入字段的值調用的。 我想找到paying_nowpaid的值的總和,以及balancepaying_now

netpaybalancepaid的值將從數據庫中獲取。 在這里,我將net_pay設置為1000。

paying_now值添加到該文本框中時,此代碼僅適用於余額,並且不會更改已paid文本框。 NaN也正在發生。 是什么賦予了?

Java腳本

   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/

您的JScript代碼基本上完成了您想要的描述(但也許我理解錯了)。

只要您在“現在的付款”字段中有一些不是數字的值(無字或字母),就會出現NaN。 為了解決這個問題,您需要先對變量paying_now的內容進行檢查,然后再使用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);' />

我已經修改了代碼。 請執行此操作,其工作正常。

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>

<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;
}

如果您只想獲得已支付的當前更改值,則只需刪除“ paid”和“ paying_now”的加法,這樣您的代碼就會看起來像。

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM