取消选中表单元素时,如何减去 javascript 变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11192862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I subtract the value of a javascript variable when form element is unchecked
提问by Keith S.
Note, this isn't my code, or the actual code in the form. This is just my testing version, but does represent what I want to do.
请注意,这不是我的代码,也不是表单中的实际代码。这只是我的测试版本,但确实代表了我想做的事情。
Given the following code, how can I get the script to subtract the passed value when the checkbox is unselected:
给定以下代码,如何在未选中复选框时让脚本减去传递的值:
<!DOCTYPE html>
<html lang="en">
<head>
<title>form test</title>
<script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script type="text/javascript">
function updateAmount(amount) {
parseInt(amount);
if ($('#amount').val()) {
parseInt(amount);
amount = amount + parseInt($('#amount').val());
}
$('#amount').val(amount);
alert('Amount: ' + $('#amount').val());
}
</script>
</head>
<body>
<form action="" method="post" name="form-test">
<input onclick="updateAmount(150)" class="inputbox" id="reservationfee0"
value="Registration Fee" type="checkbox" />
<strong>0</strong>Reservation Fee</form>
</body>
</html>
As it stands, whoever coded the function didn't take into account that people might check and uncheck that box before they submit the form (which includes more fields and other numbers that get added into the amount variable before submission). There has been a couple of reports of over-charges due to this, so I need to figure out how to stop it from happening, but I know very little about javascript/jquery.
就目前而言,编写函数的人没有考虑到人们可能会在提交表单之前选中和取消选中该框(其中包括在提交之前添加到数量变量中的更多字段和其他数字)。由于这个原因,有几个关于收费过高的报告,所以我需要弄清楚如何阻止它发生,但我对 javascript/jquery 知之甚少。
采纳答案by Manse
Try :
尝试 :
function updateAmount(amount) {
$amount = $('#amount'); // cache for later use
if ($('#reservationfee0').is(':checked')) {
$amount.val(parseInt($amount.val(), 10) + amount);
} else {
$amount.val(parseInt($amount.val(), 10) - amount);
}
}?
this checks if the checkbox
is checked using .is()
if it is then the amount is added to the current value if not its subtracted.
这检查是否checkbox
使用.is()
if it is 检查,然后将金额添加到当前值,如果不是减去它。
Also note that parseInt
takes a second parameter - radix
its recommended that you include this value to be certain of results ... docs for parseInt here
另请注意,parseInt
需要第二个参数 -radix
建议您包含此值以确保结果...... parseInt 的文档在这里
回答by MMK
can you try this
你能试试这个吗
function updateAmount(amount) {
parseInt(amount);
if ($('#reservationfee0').prop('checked')) {
amount = amount + parseFloat($('#amount').val());
}
else{
amount = parseFloat($('#amount').val())-amount;
}
$('#amount').val(amount);
alert('Amount: ' + $('#amount').val());
}
回答by Shivang
What we can do is define a variable just above the function which we can set for the first time. The code goes as follows:
我们可以做的是在我们可以第一次设置的函数上方定义一个变量。代码如下:
var chk=1;
function updateAmount(amount) {
parseInt(amount);
if ($('#amount').val() && document.getElementById('reservationfee0').checked && chk==1) {
chk=0;
parseInt(amount);
amount = amount + parseInt($('#amount').val());
}
$('#amount').val(amount);
alert('Amount: ' + $('#amount').val());
}