按键修改后如何获取HTML输入的新值? [英] How to get the new value of an HTML input after a keypress has modified it?

查看:32
本文介绍了按键修改后如何获取HTML输入的新值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML输入框

I have an HTML input box

<input type="text" id="foo" value="bar">

我已经为' keyup '事件附加了一个处理程序,但是如果我在事件处理程序期间检索输入框的当前值,则会得到原样的值,而不是会的!

I've attached a handler for the 'keyup' event, but if I retrieve the current value of the input box during the event handler, I get the value as it was, and not as it will be!

我尝试拾取' keypress '和' change '事件,这是同样的问题.

I've tried picking up 'keypress' and 'change' events, same problem.

我相信这很容易解决,但是目前我认为唯一的解决方案是在未来的几毫秒内使用短超时来触发一些代码!

I'm sure this is simple to solve, but at present I think the only solution is for me to use a short timeout to trigger some code a few milliseconds in the future!

在这些事件期间是否有获取当前值的提示?

看起来我的js文件存在缓存问题,因为稍后我检查了相同的代码,它工作得很好.我会删除问题,但不确定是否会丢掉那些发表想法的人:)

looks like I had a caching problem with my js file as I checked the same code later on and it worked just fine. I would delete the question, but not sure if that loses rep for the kind folk who posted ideas :)

推荐答案

您可以发布代码吗?我没有发现任何问题.在Firefox 3.01/safari 3.1.2上进行了以下测试:

Can you post your code? I'm not finding any issue with this. Tested on Firefox 3.01/safari 3.1.2 with:

function showMe(e) {
// i am spammy!
  alert(e.value);
}
....
<input type="text" id="foo" value="bar" onkeyup="showMe(this)" />

这篇关于按键修改后如何获取HTML输入的新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆