How do I implement onchange of <input type=“text”> with jQuery?

后端 未结 14 1058
[愿得一人]
[愿得一人] 2020-11-27 10:15

?

相关标签:
14条回答
  • 2020-11-27 11:06

    The following will work even if it is dynamic/Ajax calls.

    Script:

    jQuery('body').on('keyup','input.addressCls',function(){
      console.log('working');
    });
    

    Html,

    <input class="addressCls" type="text" name="address" value="" required/>
    

    I hope this working code will help someone who is trying to access dynamically/Ajax calls...

    0 讨论(0)
  • 2020-11-27 11:10

    You could use .keypress().

    For example, consider the HTML:

    <form>
      <fieldset>
        <input id="target" type="text" value="Hello there" />
      </fieldset>
    </form>
    <div id="other">
      Trigger the handler
    </div>
    

    The event handler can be bound to the input field:

    $("#target").keypress(function() {
      alert("Handler for .keypress() called.");
    });
    

    I totally agree with Andy; all depends on how you want it to work.

    0 讨论(0)
  • 2020-11-27 11:11

    You can do this in different ways, keyup is one of them. But i am giving example below with on change.

    $('input[name="vat_id"]').on('change', function() {
        if($(this).val().length == 0) {
            alert('Input field is empty');
        }
    });
    

    NB: input[name="vat_id"] replace with your input ID or name.

    0 讨论(0)
  • 2020-11-27 11:13

    As @pimvdb said in his comment,

    Note that change will only fire when the input element has lost focus. There is also the input event which fires whenever the textbox updates without it needing to lose focus. Unlike key events it also works for pasting/dragging text.

    (See documentation.)

    This is so useful, it is worth putting it in an answer. Currently (v1.8*?) there is no .input() convenience fn in jquery, so the way to do it is

    $('input.myTextInput').on('input',function(e){
     alert('Changed!')
    });
    
    0 讨论(0)
  • 2020-11-27 11:14

    Here's the code I use:

    $("#tbSearch").on('change keyup paste', function () {
        ApplyFilter();
    });
    
    function ApplyFilter() {
        var searchString = $("#tbSearch").val();
        //  ... etc...
    }
    
    <input type="text" id="tbSearch" name="tbSearch" />
    

    This works quite nicely, particularly when paired up with a jqGrid control. You can just type into a textbox and immediately view the results in your jqGrid.

    0 讨论(0)
  • 2020-11-27 11:14

    This worked for me. If field with name fieldA is clicked or any key entered it updates field with id fieldB.

    jQuery("input[name='fieldA']").on("input", function() {
        jQuery('#fieldB').val(jQuery(this).val());
    });
    
    0 讨论(0)
提交回复
热议问题