[英]How can I get form field values to sum up to another field value using javascript/jQuery?
I have the following html form fields that I need the sum of acct1, acct2 and acct3 to equal the value of the surcharge field. 我有以下html表单字段,我需要acct1,acct2和acct3之和等于附加费字段的值。
<input type="text" name="surcharge" id="surcharge" class="gui-input" placeholder="xx.xx">
<input type="text" name="acct1" id="acct1" placeholder="xx.xx">
<input type="text" name="acct2" id="acct2" placeholder="xx.xx">
<input type="text" name="acct3" id="acct3" placeholder="xx.xx">
I have tried several variations of this thus far: 到目前为止,我已经尝试了几种方法:
$('#surcharge').change(function(){
if( ( $('#acct1').val() + $('#acct2').val() + $('#acct3').val() ) == $('#surcharge') ){
alert("Values match Surcharge");
}
else {
alert("Values DO NOT match Surcharge");
}
});
尝试将值转换为整数
parseInt($('#acct1').val())+parseInt($('#acct2').val());
The problem is that the val()
method returns a string. 问题是val()
方法返回一个字符串。 It needs to be converted to an integer first. 首先需要将其转换为整数。 You can do that by calling Number()
and wrapping the value inside the parentheses. 您可以通过调用Number()
并将值包装在括号内来实现。
$('#surcharge').change(function(){
if( Number($('#acct1').val()) + Number($('#acct2').val()) + Number($('#acct3').val()) == $('#surcharge').val() ){
alert("Values match Surcharge");
}
else {
alert("Values DO NOT match Surcharge");
}
});
This is kind of tough to read though, so might be a better idea to break it down 虽然这很难读,所以将其分解可能是一个更好的主意
$('#surcharge').change(function() {
var total = 0;
$("#acct1", "#acct2", "#acct3").each(function() {
total += Number($(this).val());
});
if (total == $('surcharge').val()) {
});
You need to convert you field values to numbers. 您需要将字段值转换为数字。 The default input field value is a string. 默认输入字段值为字符串。 At the moment you are adding strings. 目前,您正在添加字符串。 To get a number do this for all your fields (including surcharge): 要获取编号,请对您所有的字段(包括附加费)执行以下操作:
parseFloat($('#acct1').val());
$('#surcharge,#acct1,#acct2,#acct3').keyup(function() { console.log([parseInt($('#acct1').val()),parseInt($('#acct2').val()),parseInt($('#acct3').val()), parseInt($('#surcharge').val())]); if ( ( parseInt($('#acct1').val()) + parseInt($('#acct2').val()) + parseInt($('#acct3').val()) ) == parseInt( $('#surcharge').val() ) ) { alert("Values match Surcharge"); } else { alert("Values DO NOT match Surcharge"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="acct1" value="1">+ <input id="acct2" value="1">+ <input id="acct3" value="1">= <input id="surcharge" value="3">
You should change your listener to something like blur so the event doesn't trigger with every change (key press) on the surcharge field. 您应该将您的听众更改为类似模糊的内容,这样事件不会在附加费字段上的每次更改(按键)时都触发。
Also, add a class to all of your acct input fields so you can loop by the class: 另外,将一个类添加到所有acct输入字段中,以便您可以按该类循环:
$( "#surcharge" ).blur(function() {
var total = 0;
$(".acct").each(function() {
if($(this).val()) {
total += parseFloat($(this).val());
}
});
//you may want total.toFixed(2)
if(parseFloat($(this).val()) == total.toFixed(2)) {
alert("Values match Surcharge");
} else {
alert("boooo!");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.