html <input type=“text” /> onchange event not working

后端 未结 12 2516
-上瘾入骨i
-上瘾入骨i 2020-11-30 01:13

I am trying to do some experiment. What I want to happen is that everytime the user types in something in the textbox, it will be displayed in a dialog box. I used the

相关标签:
12条回答
  • 2020-11-30 01:22

    onchange is only triggered when the control is blurred. Try onkeypress instead.

    0 讨论(0)
  • 2020-11-30 01:23

    Firstly, what 'doesn't work'? Do you not see the alert?

    Also, Your code could be simplified to this

    <input type="text" id="num1" name="num1" onkeydown="checkInput(this);" /> <br />
    
    function checkInput(obj) {
        alert(obj.value); 
    }
    
    0 讨论(0)
  • 2020-11-30 01:24

    use following events instead of "onchange"

    - onkeyup(event)
    - onkeydown(event)
    - onkeypress(event)
    
    0 讨论(0)
  • 2020-11-30 01:24

    onkeyup worked for me. onkeypress doesn't trigger when pressing back space.

    0 讨论(0)
  • 2020-11-30 01:32

    I encountered issues where Safari wasn't firing "onchange" events on a text input field. I used a jQuery 1.7.2 "change" event and it didn't work either. I ended up using ZURB's textchange event. It works with mouseevents and can fire without leaving the field:
    http://www.zurb.com/playground/jquery-text-change-custom-event

    $('.inputClassToBind').bind('textchange', function (event, previousText) {
        alert($(this).attr('id'));
    });
    
    0 讨论(0)
  • 2020-11-30 01:34

    Checking for keystrokes is only a partial solution, because it's possible to change the contents of an input field using mouse clicks. If you right-click into a text field you'll have cut and paste options that you can use to change the value without making a keystroke. Likewise, if autocomplete is enabled then you can left-click into a field and get a dropdown of previously entered text, and you can select from among your choices using a mouse click. Keystroke trapping will not detect either of these types of changes.

    Sadly, there is no "onchange" event that reports changes immediately, at least as far as I know. But there is a solution that works for all cases: set up a timing event using setInterval().

    Let's say that your input field has an id and name of "city":

    <input type="text" name="city" id="city" />
    

    Have a global variable named "city":

    var city = "";
    

    Add this to your page initialization:

    setInterval(lookForCityChange, 100);
    

    Then define a lookForCityChange() function:

    function lookForCityChange()
    {
        var newCity = document.getElementById("city").value;
        if (newCity != city) {
            city = newCity;
            doSomething(city);     // do whatever you need to do
        }
    }
    

    In this example, the value of "city" is checked every 100 milliseconds, which you can adjust according to your needs. If you like, use an anonymous function instead of defining lookForCityChange(). Be aware that your code or even the browser might provide an initial value for the input field so you might be notified of a "change" before the user does anything; adjust your code as necessary.

    If the idea of a timing event going off every tenth of a second seems ungainly, you can initiate the timer when the input field receives the focus and terminate it (with clearInterval()) upon a blur. I don't think it's possible to change the value of an input field without its receiving the focus, so turning the timer on and off in this fashion should be safe.

    0 讨论(0)
提交回复
热议问题