Why does the javascript onchange event not fire if autocomplete is on?

前端 未结 6 1071
误落风尘
误落风尘 2020-11-27 20:26

I have a textbox with an onchange event. Why does this event not fire when the user uses the autocomplete feature to populate the textbox?

I am working with Internet

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 20:32

    I found that the following jQuery (v1.10+) JavaScript does work in the case of text being auto-completed for HTML text input fields. This was tested to work reliably at least in Safari 6.1.1 on Mac OS X 10.8.5, but should work in other compliant browsers also:

    $("input:text[id=text_field_id]").bind("focus change keyup blur", function(event) {
    // handle text change here...
    });
    

    It seemed the addition of the blur event handler was the key to making this work, although the other event handlers help ensure that the event handler is called any time the text changes, whether due to an edit or new input by the browser's auto-complete feature.

    Simply replace the "input:text[id=text_field_id]" code above with the relevant selector for your desired text input field, or if using the id attribute to refer to your field, replace text_field_id with your text field's id attribute value.

提交回复
热议问题