textbox browser autocompletion options do not fire

2019-09-09 10:54发布

问题:

I have an <input type = "text"> binded with a "keypress" event, as follows (see jsfiddle here: http://jsfiddle.net/periklis/RMyc7/):

<form name = "myform" method = "post">
    <input type = "text" name = "mytext" id = "text_id"/>
    <input type = "submit"/>
</form>
<script>
$(document).ready(function() {
    $('#text_id').bind('keyup', function() {
        console.log('pressed!');
    });
});
</script>​

My problem is that if I select one of the browser recommendations for the field (previously entered values), then the keyup event won't fire. What do you suggest I should do to work around this?

Tested on chromium 17 and Firefox 10

Thanks as always

回答1:

Try binding to both keyup and change.

$('#text_id').on('keyup change', function () {
    // Code that works well for both cases, since they are
    // a bit different.
})


回答2:

I ended up setting 'autocomplete=off' to my field to prevent this from happening