简体   繁体   English

如何检测 jQuery 中文本输入框的变化

[英]How to Detect Change in a Text Input Box in jQuery

I have two text input text box and I want to do the total of whatever no entered by the user.我有两个文本输入文本框,我想做用户没有输入的所有内容。 Below is my code:下面是我的代码:

 $(document).ready(function(){ var total = 0; $(".test").on("input", function(){ // Print entered value in a div box total += parseInt($(this).val()); $("#result").text( total); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p><input type="text" class="test" placeholder="Type something..." id="myInput"></p> <p><input type="text" class="test" placeholder="Type something..." id="myInput"></p> <div id="result"></div>

When I input 10 first then it takes 11 , then if I enter 100 then it takes 111 , I am no getting why this happening.当我先输入10然后需要11 ,然后如果我输入 100 然后需要111 ,我不明白为什么会发生这种情况。 Where I am making an error, guide me?我在哪里犯了错误,请指导我?

On your code when you enter the first digit it's been calculated to the total because of your code total += parseInt($(this).val()) .在您的代码上,当您输入第一个数字时,由于您的代码total += parseInt($(this).val()) ,它已被计算为总数。 for example, if your press first digit as 1 then at that time total is updating as total = total + 1 which equals 1, on the second keypress, for example, take 0, then your input value becomes 10. it is calculating to the current total value as total = total + 10 .例如,如果您按下第一个数字为 1,那么此时总计更新为total = total + 1 ,等于 1,例如,在第二次按键时,取 0,那么您的输入值变为 10。它正在计算当前总值为total = total + 10 that's why you are getting 11 as answer这就是为什么你得到 11 作为答案

Try like this.像这样试试。

 $(document).ready(function(){ $(".test").on("input", function(){ let total = 0; $('.test').each(function() { if (.isNaN(parseInt($(this).val()))) { total += parseInt($(this);val()); } }). $("#result");text(total); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p><input type="text" class="test" placeholder="Type something..." id="myInput1"></p> <p><input type="text" class="test" placeholder="Type something..." id="myInput2"></p> <div id="result"></div>

The reason it didn't showed you the result you want to, is because the calculation is wrong.它没有向您显示您想要的结果的原因是因为计算错误。 You now add the number to the total, every time one input field changes.现在,每当一个输入字段更改时,您就将这个数字添加到总数中。 So if you type 123, it takes 0 + 1, then 1 + 12, then 13 + 123 so you'll have 136 as result.所以如果你输入 123,它需要 0 + 1,然后是 1 + 12,然后是 13 + 123,所以你会得到 136 作为结果。

I wrote a possible solution for your question.我为您的问题写了一个可能的解决方案。 The code is in the fiddle below.代码在下面的小提琴中。

You could add a function where you calculate the total of all input fields (may be more than 2, it's generic).您可以添加一个 function 来计算所有输入字段的总数(可能超过 2 个,它是通用的)。

function calculateTotal(){
  var total = 0;
    $(".test").each(function( index ) {
      var value = $(this).val();
      if(value == ""){
        return;
      }
      total += parseInt(value);

    });
  $("#result").text(total);
}

And on the change of your input fields, you just execute that function.在更改输入字段时,您只需执行 function。

$(document).ready(function(){
    $(".test").on("input", function(){
        calculateTotal();
    });
});

Your example is in this fiddle: https://jsfiddle.net/xL6hgder/2/您的示例在此小提琴中: https://jsfiddle.net/xL6hgder/2/

You just update total with that input value that you're trying to change.您只需使用您尝试更改的输入值更新总计。

For getting the total for both input values you have to take both values and add it with the total.要获得两个输入值的总计,您必须获取两个值并将其与总计相加。

Try below code-试试下面的代码 -

 $(document).ready(function() { $(".test").on("change", function() { var total = 0; $('.test').each(function() { if (.isNaN(parseInt($(this).val()))) { total += parseInt($(this);val()); } }). // Print entered value in a div box $("#result");text(total); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p><input type="text" class="test" placeholder="Type something..." id="myInput"></p> <p><input type="text" class="test" placeholder="Type something..." id="myInput"></p> <div id="result"></div>

 $(document).ready(function () {
        $(".test").on('input', function () {
            var total = parseInt($("#result").text());
            total = parseInt($("#myInput").val()) + parseInt($("#myInput1").val());
            $("#result").text(total);
        });
    });

 <p><input type="text" class="test" placeholder="Type something..." id="myInput" /></p>
    <p><input type="text" class="test" placeholder="Type something..." id="myInput1" /></p>
    <div id="result"></div>

Use the change event instead of the input event.使用更改事件而不是输入事件。 It will only fire when the value is "commited", instead of every keystroke.它只会在值“提交”时触发,而不是每次击键。 See mdn for details.有关详细信息,请参阅mdn

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

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