将重点放在onchange事件中的文本框中 [英] Putting focus on a textbox in onchange event
问题描述
我试图得到它,以便当一个值被放入一个文本框时,焦点将保留在文本框上(并且生成中会显示警报)。我试图让这个工作在Firefox 3.5.7没有运气。
如果文本框在 onchange
中具有某个值,那么该怎么做才能保持对焦/重新聚焦在文本框上?
现场示例是 http:// jsbin .com / ipina
< body>
输入你的名字:< input type =textname =fnameid =fnameonchange =
if(this.value =='foo'){
this .select();
this.focus();
}
/>
< / body>
此外,在执行此代码时,我不会在错误控制台中收到任何JavaScript错误或警告。根据 Javascript onchange在IE和FireFox中不同我需要在onchange事件发生后设置焦点,所以我不得不这样结束:
输入您的姓名:< input type =textname =fnameid =fnameonblur =
if(this。 value =='foo'){
alert('bah');
setTimeout('document.getElementById(\'fname\')。focus(); document.getElementById(\' fname\')。select();',0);
}
/>
还有,当焦点丢失时,我不得不抓住它,而不一定在文本更改时,所以我不得不使用onblur而不是onchange。
I am trying to get it so that when a certain value is put into a textbox, the focus will stay on the textbox(and an alert will be shown in production). I am trying to get this to work in Firefox 3.5.7 with no luck.
How can I make it so when a textbox is a certain value at onchange
that it will stay focused/refocus on the textbox?
Live example is at http://jsbin.com/ipina
<body>
Enter your name: <input type="text" name="fname" id="fname" onchange="
if(this.value=='foo'){
this.select();
this.focus();
}
" />
</body>
Also, I don't get any javascript errors or warnings in the Error Console on executing this code.
According to Javascript onchange different in IE and FireFox I needed to set the focus after the onchange event occurs, so I had to end up with something like this:
Enter your name: <input type="text" name="fname" id="fname" onblur="
if(this.value=='foo'){
alert('bah');
setTimeout('document.getElementById(\'fname\').focus();document.getElementById(\'fname\').select();',0);
}
" />
And also I had to catch it when the focus was lost, not necessarily when the text was changed, so I had to use onblur instead of onchange.
Live: http://jsbin.com/ofeva
这篇关于将重点放在onchange事件中的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!