文本框之间的Javascript计算
我的Javascript函数文本框之间的Javascript计算,javascript,html,Javascript,Html,我的Javascript函数aa()是用paying\u now输入字段的值调用的。我想求立即付款和已付款的值之和,以及余额和立即付款的差值 将从数据库中获取netpay、balance和paid的值。这里,我将净工资设置为1000,作为一个例子 此代码仅适用于余额,并且在应向其添加立即付款值时,不会更改已付款文本框。南也在发生。有什么好处 Javascript HTML 网付: 支付: 平衡: 立即付款: 值得一提的是,这里有一个包含以下代码的JSFIDLE:我已经修改了代码。请执行此操作
aa()
是用paying\u now
输入字段的值调用的。我想求立即付款
和已付款
的值之和,以及余额
和立即付款
的差值
将从数据库中获取netpay
、balance
和paid
的值。这里,我将净工资设置为1000,作为一个例子
此代码仅适用于余额,并且在应向其添加立即付款
值时,不会更改已付款
文本框。南也在发生。有什么好处
Javascript
HTML
网付:
支付:
平衡:
立即付款:
值得一提的是,这里有一个包含以下代码的JSFIDLE:我已经修改了代码。请执行此操作,它工作正常
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代码中有一些小错误,请也修改一下。例如,在某些地方,关闭标记丢失。将隐藏字段放在
和
中。如果您只想获得当前已支付的更改值,您只需删除“已支付”和“立即支付”的加法,这样您的代码就会像这样
<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>
功能aa(立即付款)
{
var net_pay=document.getElementById(“net_pay”).value;
var pay_now=document.getElementById('pay_now').value;
var paid=document.getElementById('paid_hidden')。值;
var balance=document.getElementById('balance_hidden')。值;
如果(立即付款=“”)
{
警报(“价值要求”);
document.getElementById('paying_now').focus();
document.getElementById('paid\u hidden')。value=paid;
document.getElementById('balance_hidden')。value=balance;
回来
}
if(parseInt(立即支付)>parseInt(净支付))
{
document.getElementById('paying_now')。值=0;
document.getElementById('paid')。value=paid;
document.getElementById('balance')。value=balance;
提醒(“现在支付的金额不能高于网付”);
回来
}
else if(parseInt(立即付款)>parseInt(余额))
{
document.getElementById('paying_now')。值=0;
document.getElementById('paid')。value=paid;
document.getElementById('balance')。value=balance;
提醒(“现在支付的金额不能高于余额金额”);
回来
}
document.getElementById('paid').value=parseInt(立即支付);
document.getElementById('balance')。value=parseInt(balance)-parseInt(立即付款);
}
您的JScript代码基本上实现了您所描述的您希望它实现的功能(但可能我理解错了)
当您的paying now字段中有一些值不是数字(即nothing或字母)时,NaN就会出现。为了解决这一问题,您需要先检查变量paying_now
的内容,然后再对其进行算术运算,即使用函数:
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);' />
Netpay:
支付:
余额:
立即付款:
您好,您的html格式看起来非常不正确。我已经创建了JSFIDLE来使用:-我现在试图找到解决方案您的脚本即使在付费框中也能正常工作,当付费文本框没有值时,NAN就会出现,在这种情况下,parseInt()会出现返回NAN,并使用支付值添加字符串“NAN”,然后parse最终返回不代表数字的NAN。无法编辑我的上一条评论-我之前的JSFIDLE工作不正常,我对其进行了修改:它可以工作,但是,如何在字段中避免NAN?当我对它进行更改时,它会显示出来,这是一个未来的友好提示:它可能有助于解释最初的问题是什么以及您是如何解决的,而不仅仅是提供解决方案,这样,在类似情况下,其他人就不会再遇到此类问题。未来的一个友好提示:它可能有助于解释最初的问题是什么以及您是如何解决的,而不仅仅是提供解决方案,这样在类似情况下,其他人就不会再遇到此类问题。
if (isNaN(paying_now) == false) {
alert ("Not a number!");
return;
}
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);' />
<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>
if(isNaN(parseInt(paying_now)))
{paying_now=0;
}