使用 javascript 对提交执行计算时遇到错误。pay_amount 输入提交“1”,total_charged 提交“4.95”,无论pay_amount 的值是多少。
这是html:
<form method="POST" action="example-post-url.php" autocomplete="off" name="form" id="form">
<input type="text" id="pay_amount" name="pay_amount" value="1,794.00"/>
<input type="hidden" id="total_charged" name="total_charged" />
<input type="submit" value="submit" onclick="submitCard();" id="submitButton"/>
</form>
这是javascript:
<script>
function submitCard(){
var payment = parseFloat(document.getElementById('pay_amount').value);
var fee = ((payment*0.035)+3.95);
var total = payment + fee;
document.getElementById('total_charged').value = total.toFixed(2);
document.form.submit();
}
</script>
我尝试用下面的代码替换 var payment,认为这是逗号的问题,但我仍然遇到问题。
var payment = document.getElementById('pay_amount').value.replace(/,/g, '');
这里到底发生了什么?