How to call a JS function, on data selection in <input type=“text”> from AUTOCOMPLETE list?

自作多情 提交于 2019-12-13 04:41:48

问题


I have a form with a text box: wherever value changed by typing, showing alert message by KeyPress event, but it does not work if "Use AutoComplete for Form" is enable in IE and then whenever user double click and select text from a list of "possible entries, user have typed before".


回答1:


Use the client-side “onpropertychange” event for this purpose:

Here is the solution for both the standard and DevExpress (it looks like you are using it) textboxes:

<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
    //DevExpress ASPxTextBox
    function OnInit(s, e) {
        var inputElement = s.GetInputElement();
        ASPxClientUtils.AttachEventToElement(inputElement, "propertychange", function (event) {
            if (event.propertyName === "value") {
                alert("Value Changed");
                alert(event.srcElement.value);
            }
        });
    }

    //Standard Input
    $(document).ready(function () {
        var inputElement = document.getElementById("txt");
        //var inputElement = document.getElementById('<%=txt.ClientID%>');
        inputElement.attachEvent("onpropertychange", function (event) {
            if (event.propertyName === "value") {
                alert("Value Changed");
                alert(event.srcElement.value);
            }
        });
    });
</script>

<dx:ASPxTextBox ID="txtDX" runat="server" Width="170px">
    <ClientSideEvents Init="OnInit" />
</dx:ASPxTextBox>
<br />
<input id="txt" type="text" runat="server" />
<br />
<asp:Button ID="btn" runat="server" Text="Button" />



回答2:


U want some thing like this.see
http://jqueryui.com/demos/autocomplete/




回答3:


Take a look at these links:
http://www.phdesign.com.au/programming/ie-autocomplete-doesnt-fire-onchange-event-handler/

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



来源:https://stackoverflow.com/questions/8834030/how-to-call-a-js-function-on-data-selection-in-input-type-text-from-autocom

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