简体   繁体   English

Onkeypress更新输入落后1步

[英]Onkeypress update input is 1 step behind

I am trying to update an input with 2 other inputs. 我正在尝试使用其他2个输入来更新输入。 For example 例如

Amount : (user inputs value) 金额:(用户输入值)

Fee: (value is already set -- disabled value: 0.0002) 费用:(已设置值-禁用值:0.0002)

Total: (amount - fee) worked out via javascript below 总计:(金额-费用)通过以下javascript计算出

$('#amount').keypress(function() {
var total = $('#amount').val() - $('#fee').val();
if(typeof total != 'undefined'){ $('#total').val(total) }});

When I 'keypress' on my input with a number, it is one step behind updating the input with total. 当我用数字“敲击”输入时,这是将总数更新为输入的第一步。

Say for an example if I type 1 in the input amount, the total will be -0.0002(the fee) when I press 1 again, it will be 0.9998 (even though the amount input is now 11 so on so forth.) 举个例子,如果我输入1,那么当我再次按1时,总数将是-0.0002(费用),它将是0.9998(即使现在输入的数量是11,依此类推)。

It's cause of you're using keypress which consists of 2 events keydown and keyup , so your function launches on the first one - keydown , so you get your values, as you said "one step behind", cause when the keydown event is fired your input field didn't recieve the pressed key value yet. 这是因为您使用的keypress包含2个事件keydownkeyup ,所以您的函数在第一个事件上启动keydown ,因此您得到的值(如您所说的“落后一步”)是在触发keydown事件时引起的您的输入字段尚未收到按键值。 Use keyup and you will get your result: 使用keyup ,您将得到结果:

$('#amount').keyup(function() {
    var total = $('#amount').val() - $('#fee').val();
    if(typeof total != 'undefined') $('#total').val(total) 
});

Fiddle 小提琴

True, because when function is called the field is not updated with the value 正确,因为调用函数时,该字段未使用值更新

The event is called in this steps 在此步骤中调用该事件

  • Keydown 按键
  • Keypress 按键
  • updateview 更新视图
  • Keypress 按键
  • updateview 更新视图
  • keyup 键控

So if you can change this event to keyup then you will get the latest value. 因此,如果您可以将此事件更改为keyup ,则将获得最新值。

The posted answers are good answers, but the best event to use here is the oninput event, because it's faster. 发布的答案是很好的答案,但此处使用的最佳事件是oninput事件,因为它更快。

$('#amount').on('input', function() {
    var total = $('#amount').val() - $('#fee').val();

    if(typeof total != 'undefined'){
        $('#total').val(total);
    }
});

Supported by all browsers: browser support . 所有浏览 器都支持 浏览器支持 It's actually better supported than onchange and other events like onkeyup . 实际上,它比onchange和其他事件(例如onkeyup得到更好的支持。 You can compare that on https://caniuse.com/ . 您可以在https://caniuse.com/上进行比较。

You can even use this event for other input elements as well. 您甚至还可以将此事件用于其他输入元素。 Like checkboxes, radio buttons and the select element. 像复选框,单选按钮和选择元素一样。

It's fast. 它很快。 So you can combine this with search queries to make your search results return faster. 因此,您可以将其与搜索查询结合使用,以使搜索结果更快地返回。

Please note: since this is not a keyboard event, there won't be a keyCode or any other keyboard related value in the event. 请注意:由于这不是键盘事件,因此该事件中不会有keyCode或任何其他与键盘相关的值。


Read more 阅读更多

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

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