How to detect a textbox's content has changed

后端 未结 15 2033
猫巷女王i
猫巷女王i 2020-11-22 16:10

I want to detect whenever a textbox\'s content has changed. I can use the keyup method, but that will also detect keystrokes which do not generate letters, like the arrow ke

15条回答
  •  执念已碎
    2020-11-22 16:30

    Use the textchange event via customized jQuery shim for cross-browser input compatibility. http://benalpert.com/2013/06/18/a-near-perfect-oninput-shim-for-ie-8-and-9.html (most recently forked github: https://github.com/pandell/jquery-splendid-textchange/blob/master/jquery.splendid.textchange.js)

    This handles all input tags including , which does not always work with change keyup etc. (!) Only jQuery on("input propertychange") handles

    JS Bin test

    This also handles paste, delete, and doesn't duplicate effort on keyup.

    If not using a shim, use jQuery on("input propertychange") events.

    // works with most recent browsers (use this if not using src="...splendid.textchange.js")
    
    $('textarea').on("input propertychange",function(){ 
      yourFunctionHere($(this).val());    
    });  
    

提交回复
热议问题