开发者

TextField 'Change' Event only triggers on blur

Normally, the Change event will trigger after the TextField loses its focus (on blur).

开发者_StackOverflow社区

But I need it to trigger as soon as the value of the field changes, without the need to lose the focus on the field.

The KeyListener doesn't cut it, because the value may come, for example, from a barcode scanner.

Is there any way to do achieve this?

Thanks in advance!


I haven't worked with ext-gwt, but this is what you have to do: You have to use the KeyListener AND add a listener for ONPASTE. The 'Change' event is provided by the browser, and it is triggered only while focus goes away (during a blur), and if text has changed.


I don't think there is an event that works cross-browser for all situations. So the "poor man's solution" is to poll the text field every second or so. In fact, such a test can be performed pretty quickly, and if you don't use it on lots of text fields at once, you should be fine.

You can use my little example code if you like (it works on a plain GWT TextBox, but it should be straightforward to adapt for an Ext-GWT TextField)

@Override
public void onModuleLoad() {

    final TextBox textBox = new TextBox();
    final int delayMilliseconds = 1000;

    Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {

        private String previousValue = "";

        @Override
        public boolean execute() {

            final String newValue = textBox.getValue();
            if (!previousValue.equals(newValue)) {
                try {
                    valueChanged();
                } finally {
                    previousValue = newValue;
                }
            }
            return true;
        }

        private void valueChanged() {
            // React on the change
            Window.alert("Value changed");
        }

    }, delayMilliseconds);

    RootPanel.get().add(textBox);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜