我对表单中的一些代码有疑问。我正在尝试从用户那里获取值(value)(美元金额)并将其解析为整数。然后我需要执行计算以将 2.9% 添加到其中,并将 .30 添加到产品中。
似乎问题出在解析中...因为我在控制台中收到错误提示 $sendAmount.val is not a function
[当我输入 $sendAmount.val ()
]。然而,如果我提交 $userAmount.val()
,它会返回用户提交的美元金额(在一个字符串中)。
请记住,$userAmount
是用户输入的内容,
$sendAmount
是发送到 Paypal 的金额。
如有任何帮助,我们将不胜感激……我一直在努力让它发挥作用,但一直空无一人。我对 parseInt 没有太多经验。
这是我的代码:
var $sendAmount = $("#payAMT");
var $userAmount = $("#valInput");
//Update the Amount
function $convFee() {
$sendAmount = parseInt($userAmount) * 1.029 + 0.30;
};
$agree.keyup($convFee);
$agree.click($convFee);
<div id="paypalWrap">
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="new">
<input type="hidden" name="amount" id="payAMT" value="0.00">
<input type="hidden" name="currency_code" value="USD">
<p>
<label for="os0" type="hidden" name="on0" value="Name:">Name:</label>
<input type="text" name="os0" maxlength="30" id="name">
</p>
<p>
<label for="os1" type="hidden" name="on1" value="Invoice Number:">Invoice Number:
<br />
<i>(Reference must be correct to get credit applied to your account)</i>
</label>
<input type="text" name="os1" maxlength="50" id="invoice">
</p>
<p>
<label for="os2" type="hidden" name="on2" value="Amount:">Amount being paid:</label>
<input type="text" name="os2" id="valInput" maxlength="15" placeholder="ex: 10.00 (not $10.00)">
</p>
<p>
<input type="checkbox" name "agreeCheck" id="agreeCheck" />
<label for="agreeCheck" type="hidden" name="agreeStatement" id="agreeStatement">
I understand and accept that I will be charged a convenience fee ($0.30 + 2.9% of transaction).
</label>
</p>
<input id="send" type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" disabled="disabled" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
</form>
</div>
最佳答案
这一行有两次同样的问题:
$sendAmount = parseInt($userAmount) * 1.029 + 0.30;
这两个变量都是 DOM 元素,而不是数字。您需要将它们作为项目进行交互,特别是它们的值元素(可以解析为数字的字符串)。
您需要从第一个获取值,并设置第二个的值,例如:
$sendAmount.val(parseInt($userAmount.val) * 1.029 + 0.30);
关于javascript - 解析 Var 并执行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30127168/