简体   繁体   English

JavaScript实时验证和计算2个或更多输入文本字段

[英]JavaScript real time validation and calculations 2 or more input text fields

I want to do JavaScript real time calculations oninput, without the need for a submit button. 我想在输入时执行JavaScript实时计算,而无需提交按钮。

I know how to do real time calculations in JavaScript when I have 1 input text field. 当我有1个输入文本字段时,我知道如何在JavaScript中进行实时计算。 I use the oninput event for the input text field. 我将oninput事件用于输入文本字段。

But what about when I have 2 text fields? 但是,当我有2个文本字段时该怎么办?

I want it to do something like this link, where it validates and calculates without the need for a submit button: 我希望它做类似此链接的操作,它无需输入按钮即可进行验证和计算:

https://www.easycalculation.com/algebra/modulo-calculator.php https://www.easycalculation.com/algebra/modulo-calculator.php

Take the following code for example: 以以下代码为例:

// input
var a = document.getElementById("a").value; 
var b = document.getElementById("b").value;

// calculation
var result = a * b;

// display result
document.getElementById("result").value;

Since there are 2 input text fields (a and b), I want it to do the instant/real time calculations only AFTER the user has inputted valid data in BOTH text fields. 由于有2个输入文本字段(a和b),因此我希望它仅在用户在两个文本字段中都输入了有效数据之后才进行即时/实时计算。

But I also want it to do real time calculations if the user changes either field. 但是,如果用户更改任一字段,我也希望它进行实时计算。 So if they input "a" and "b" it gives the results, but if they change "a" then it immediately gives new results without them having to touch "b" at all. 因此,如果他们输入“ a”和“ b”,它将给出结果,但是如果他们更改“ a”,则它将立即给出新结果,而他们根本不必触摸“ b”。

How do you suggest I go about doing this? 您如何建议我继续这样做? Because I dont want the answer to keep showing up as Zero right after typing in the first text field. 因为我不希望答案在第一个文本字段中输入后立即显示为零。 I want it to wait until both fields have numbers inputted and validated before it starts real time calculation. 我希望它等到两个字段都输入并验证了数字后才开始实时计算。 I will be adding validating code to this as well. 我还将为此添加验证代码。

thanks 谢谢

Just try and formulate your problem so that the computer can understand it. 只需尝试解决您的问题,以便计算机可以理解。

I'll do just some pseudo-code. 我将只做一些伪代码。 So you want to calculate something: 所以你想计算一下:

function calculate (valueA, valueB) {
    ... do something ...
    ... output result ...
}

You want to check, if both fields are valid, and only then do the calculation and output: 您要检查两个字段是否均有效,然后才进行计算和输出:

function checkFields (fieldA, fieldB) {
    if (fieldA.value.length > 0) { // if it is empty, there is no input
        ... do some additional checking ...
        if (fieldB.value.length > 0) { // if it is empty, there is no input
            ... do some additional checking ...
            ... if all went well: calculate (fieldA.value, fieldB.value);
        }
    }
}

then bind your checkFields to both input fields, and the computer understands. 然后将您的checkFields绑定到两个输入字段,计算机就会理解。

you should write a function like validate() where you have to check the value of both the inpute fields if its valid then calculate result otherwise show a warning message above the field which is either empty or having wrong value 您应该编写类似validate()的函数,在其中必须检查两个inpute字段的值是否有效,然后计算结果,否则在该字段上方显示警告消息,该消息可能为空或值错误

You have to call the validate function on onchange event of both the inputs 您必须在两个输入的onchange事件上调用validate函数

This is not exactly how I would write this in a production evvironment. 这并不是我在生产环境中写的方式。 but this should at least give you a good start for what you are looking for - very basic functionality you described. 但这至少应该为您寻找的东西提供一个良好的开端-您所描述的非常基本的功能。

<form>
    <input id='a' class='ab' type='text' name='valA'>
    <input id='b' class= 'ab' type='text' name='valB'>
</form>

// JS code below with this markup //下面的带有此标记的JS代码

var someCompositeGroup = document.getElementsByClassName("ab");

function validateForm(){

    // add stuff here 
    var inputVal = 0; 
    var aVal = someCompositeGroup[0] && someCompositeGroup[0].value;
    var bVal = someCompositeGroup[1] && someCompositeGroup[1].value;

    if (aVal && bVal && !isNaN(aVal) && !isNaN(bVal)) {
        inputVal = aVal * bVal; 
        // only update here - 
       console.log(inputVal);
    } else {
        console.log(' not ready to calculate yet '); 
    }
}

for (var i = 0; i < someCompositeGroup.length; i++) {
    someCompositeGroup[i].addEventListener('keyup', validateForm);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM