jQuery 'if .change() or .keyup()'

后端 未结 7 670
说谎
说谎 2020-12-13 05:26

Using jQuery i would like to run a function when either .change() or .keyup() are raised.

Something like this.

if ( jQuery(         


        
相关标签:
7条回答
  • 2020-12-13 05:52

    You could subscribe for the change and keyup events:

    $(function() {
        $(':input').change(myFunction).keyup(myFunction);
    });
    

    where myFunction is the function you would like executed:

    function myFunction() {
        alert( 'something happened!' );
    }
    
    0 讨论(0)
  • 2020-12-13 05:53

    Do this.

    $(function(){
        var myFunction = function()
        {
            alert("myFunction called");
        }
    
        jQuery(':input').change(myFunction).keyup(myFunction);
    });
    
    0 讨论(0)
  • 2020-12-13 05:53

    That's not how events work. Instead, you give them a function to be called when they happen.

    $("input").change(function() {
        alert("Something happened!");
    });
    
    0 讨论(0)
  • 2020-12-13 05:56

    you can bind to multiple events by separating them with a space:

    $(":input").on("keyup change", function(e) {
        // do stuff!
    })
    

    docs here.

    hope that helps. cheers!

    0 讨论(0)
  • 2020-12-13 05:59

    If you're ever dynamically generating page content or loading content through AJAX, the following example is really the way you should go:

    1. It prevents double binding in the case where the script is loaded more than once, such as in an AJAX request.
    2. The bind lives on the body of the document, so regardless of what elements are added, moved, removed and re-added, all descendants of body matching the selector specified will retain proper binding.

    The Code:

    // Define the element we wish to bind to.
    var bind_to = ':input';
    
    // Prevent double-binding.
    $(document.body).off('change', bind_to);
    
    // Bind the event to all body descendants matching the "bind_to" selector.
    $(document.body).on('change keyup', bind_to, function(event) {
        alert('something happened!');
    });
    

    Please notice! I'm making use of $.on() and $.off() rather than other methods for several reasons:

    1. $.live() and $.die() are deprecated and have been omitted from more recent versions of jQuery.
    2. I'd either need to define a separate function (therefore cluttering up the global scope,) and pass the function to both $.change() and $.keyup() separately, or pass the same function declaration to each function called; Duplicating logic... Which is absolutely unacceptable.
    3. If elements are ever added to the DOM, $.bind() does not dynamically bind to elements as they are created. Therefore if you bind to :input and then add an input to the DOM, that bind method is not attached to the new input. You'd then need to explicitly un-bind and then re-bind to all elements in the DOM (otherwise you'll end up with binds being duplicated). This process would need to be repeated each time an input was added to the DOM.
    0 讨论(0)
  • 2020-12-13 06:00

    keyup event input jquery

    For Demo

    $(document).ready(function(){  
        $("#tutsmake").keydown(function(){  
            $("#tutsmake").css("background-color", "green");  
        });  
        $("#tutsmake").keyup(function(){  
            $("#tutsmake").css("background-color", "yellow");  
        });  
    }); 
    <!DOCTYPE html>  
    <html>  
    <title> jQuery keyup Event Example </title>
    <head>  
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    </head>  
    <body>  
    Fill the Input Box: <input type="text" id="tutsmake">  
    </body>  
    </html>   

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