窗口显示时让字段获得焦点

这是Ext群里一位大虾提供的解决方案。

通常情况下,在窗口显示时,也就是在窗口的show事件中,直接使用focus方法让字段获得焦点,会因为浏览器内部处理而丢失焦点。

解决办法就是延迟执行focus,在API中,Ext.form.field.Base的focus方法的说明如下:

focus( [[Boolean](http://localhost/ext4/docs/index.html#%21/api/Boolean) selectText], [[Boolean](http://localhost/ext4/docs/index.html#%21/api/Boolean)/[Number](http://localhost/ext4/docs/index.html#%21/api/Number) delay] ) : Ext.Component

Try to focus this component.

Parameters

If applicable, true to also select the text in this component

Delay the focus this number of milliseconds (true for 10 milliseconds).

Returns

The focused Component. Usually this Component. Some Containers maydelegate focus to a descendant Component (Windows can do this through theirdefaultFocus config option.

方法带两个参数,第一个参数的作用是用来选中文本的,第二个参数就是延迟执行的时间,因而只要设置第二个参数,就可以实现效果了。

例如:

form.findField("第一个字段名字").focus(false,100);

这句中的form表示的是Ext.form.Basic的实例。在focus方法,设置了延迟时间为100微秒,也就是在延迟100微秒后才将焦点转移到第一个输入字段,这样可以很好的解决该问题。

文章导航