TextField 'Change' Event only triggers on blur

限于喜欢 提交于 2019-12-10 14:56:23

问题


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

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!


回答1:


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.




回答2:


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);
}


来源:https://stackoverflow.com/questions/5915997/textfield-change-event-only-triggers-on-blur

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!