简体   繁体   English

如何使用javascript / jQuery获取表单字段值以求和另一个字段值?

[英]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!");
   }

});

And here's a JSFiddle demo. 这是一个JSFiddle演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jQuery-UI datepicker获取表单输入字段以使用其他字段中的年份? - How can I get a form input field using jQuery-UI datepicker to use the year from another field? 如何使用jQuery / JavaScript将每个字符添加到表单字段中 - How can I get each character added in a form field using jQuery/JavaScript 如何使用 Javascript 对表单中不同字段类型的值求和 - How to sum the values of different field types in a form using Javascript 我如何使用jQuery的JavaScript获取ListView项目模板内的隐藏字段值 - How can i get the hidden field value inside a listview item template using javascript of jquery 表单字段值在jQuery中求和 - Form field values sum in jQuery 我如何在php中获取表单字段的值(字段是通过javascript创建的)? - how can i get the value of the field of the form (field is created from javascript) in php? 如何使用javascript和/或jquery添加表单字段值? - How to add form field values using javascript and/or jquery? 如何使用jQuery将单选按钮的值传递给另一个表单字段? - How to pass value of radio button to another form field using jquery? 如何使用 JQuery 获取特定的表单字段值 - How to get the Specific Form Field value using JQuery 如何在javascript中获取Contact Form 7字段值? - How to get Contact Form 7 field values in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM