最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Textbox Event - Stack Overflow

programmeradmin3浏览0评论

I am facing problem with input text type (ie. Text Box).

I have written a function used by the onkeyup event on a Text Box. The line looks like this:

<input type='TEXT' value='abc' onkeyup='changeSomething( this );'>

But now I am facing problem that when user selects values from the previously entered values, I am not getting any event when user selects any previously entered values from the drop down (edit: I believe he is referring to browser autocomplete here).

Does anyone have solution for this? :)

I am facing problem with input text type (ie. Text Box).

I have written a function used by the onkeyup event on a Text Box. The line looks like this:

<input type='TEXT' value='abc' onkeyup='changeSomething( this );'>

But now I am facing problem that when user selects values from the previously entered values, I am not getting any event when user selects any previously entered values from the drop down (edit: I believe he is referring to browser autocomplete here).

Does anyone have solution for this? :)

Share Improve this question edited Jun 16, 2009 at 11:58 cgp 41.4k12 gold badges105 silver badges131 bronze badges asked Jun 16, 2009 at 11:51 PradeepPradeep 0
Add a comment  | 

2 Answers 2

Reset to default 12

use onchange instead of onkeyup in this case

see: http://www.w3schools.com/jsref/event_onchange.asp

e.g.

<input type='text' value='abc' onchange='changeSomething(this);' />

to get around this

EDIT Two things:
1) Autocomplete values can be selected using arrow keys and enter/tab, and by using the mouse. The arrow keys/enter.tab fire the onkeyup events... clicking in the autocomplete box does not, however, fire the onclick event.
2) The onchange event fires as soon as focus is lost IF the content has changed. Focus is not lost when selecting autocomplete values.

Essentially, there does not appear to be any way to reasonably guarantee the event will be processed the way you want.

First off, do you really need to listen to every keystroke? Secondly, would you be better served by turning off autocomplete? (e.g. <input type='text' value='abc' autocomplete='off' onkeyup='changeSomething(this);' />)

Here's a solution which polls the element periodically for any changes

<script type="text/javascript">

var changeWatcher = {
    timeout: null,
    currentValue: '',
    watchForChange: function( el ) {
        if( el.value != this.currentValue ) {
            this.changed( el );
        }
        this.timeout = setTimeout( function() {
             changeWatcher.watchForChange(el)
        }, 200 );
    },
    cancelWatchForChange: function() {
        clearTimeout( this.timeout );
        this.timeout = null;
    },
    changed: function( el ) {
        this.currentValue = el.value;
        // do something with the element and/or it's value
        //console.log( el.value );
    }
}

</script>
<input type='text' value='abc' onfocus='changeWatcher.watchForChange(this)' onblur='changeWatcher.cancelWatchForChange()' onchange='changeWatcher.changed(this)' />
发布评论

评论列表(0)

  1. 暂无评论