jQuery/JS監(jiān)聽input輸入框值變化實(shí)例
input事件:
onchange:
1、要在 input 失去焦點(diǎn)的時(shí)候才會觸發(fā);
2、在輸入框內(nèi)容變化的時(shí)候不會觸發(fā)change,當(dāng)鼠標(biāo)在其他地方點(diǎn)一下才會觸發(fā);
3、onchange event 所有主要瀏覽器都支持;
4、onchange 屬性可以使用于:<input>, <select>, 和 <textarea>。
<script> function change(){ var x=document.getElementById("password"); x.value=x.value.toUpperCase();<br data-filtered="filtered"> console.log("出發(fā)了") } </script> </head> <body> 輸入你的密碼: <input type="text" id="password" onchange="change()"> </body>
oninput:
1、在用戶輸入時(shí)觸發(fā),它是在元素值發(fā)生變化時(shí)立即觸發(fā);
2、該事件在 <input> 或 <textarea> 元素的值發(fā)生改變時(shí)觸發(fā)。
3、缺陷:從腳本中修改值不會觸發(fā)事件。從瀏覽器下拉提示框里選取值時(shí)不會觸發(fā)。IE9 以下不支持,所以IE9以下可用onpropertychange 事件代替。
JS: <input type="text" id="password" oninput="change()">
jQuery: $("#password").on('input propertychange', change);
onpropertychange:
1、會實(shí)時(shí)觸發(fā),會在元素的屬性改變時(shí)就觸發(fā)事件。當(dāng)元素disable=true時(shí)不會觸發(fā)
2、缺陷:只在IE 下支持,其他瀏覽器不支持,用oninput來解決。
<input type="text" id="password" oninput="onpropertychange()">
jQuery:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>RunJS</title> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> </head> <body> <input type="text" id="password" autoComplete='off'> <script type="text/javascript"> $(function(){ $('#password').bind('input propertychange', function() { <br data-filtered="filtered"> console.log('在實(shí)時(shí)觸發(fā)?。。?) $('#result').html($(this).val().length); <br data-filtered="filtered"> $(this).val().length != 0 ? $("#login").css("background-color", "#086AC1") : $("#login").css("background-color", "#529DE0") }); }) </script> </body> </html>
JavaScript;
<script type="text/javascript"> // Firefox, Google Chrome, Opera, Safari, Internet Explorer from version 9 function OnInput (event) { alert ("The new content: " + event.target.value); } // Internet Explorer function OnPropChanged (event) { if (event.propertyName.toLowerCase () == "value") { alert ("The new content: " + event.srcElement.value); } } </script> <input type="text" oninput="OnInput (event)" onpropertychange="OnPropChanged (event)" value="Text field" />
以上就是本次介紹的全部相關(guān)知識點(diǎn),感謝大家的學(xué)習(xí)和對腳本之家的支持。
相關(guān)文章
jquery load()在firefox(火狐)下顯示不正常的解決方法
覺得應(yīng)該是由于 直接將www.baidu.com的內(nèi)容放到div中,對于較嚴(yán)格的FireFox可能不會處理用cssviewter查看處理后的頁面源碼果然發(fā)現(xiàn)div中為空2011-04-04實(shí)例詳解jQuery的鏈?zhǔn)骄幊田L(fēng)格
jQuery中的鏈?zhǔn)讲僮?,它讓代碼變得更有層次更簡潔,所以這篇文章主要給大家介紹了關(guān)于jQuery鏈?zhǔn)骄幊田L(fēng)格的相關(guān)資料,需要的朋友可以參考下2021-06-06jquery實(shí)現(xiàn)用戶信息修改驗(yàn)證輸入方法匯總
這篇文章主要介紹了jquery實(shí)現(xiàn)用戶信息修改驗(yàn)證輸入方法,實(shí)例匯總了jquery常用的提交、驗(yàn)證、判定、修改等相關(guān)技巧,非常實(shí)用,需要的朋友可以參考下2015-07-07利用jquery實(shí)現(xiàn)實(shí)時(shí)更新歌詞的方法
這篇文章主要給大家介紹了如何利用jquery實(shí)現(xiàn)實(shí)時(shí)更新歌詞的方法,文中給出了詳細(xì)的實(shí)現(xiàn)思路和示例代碼,對大家的參考借鑒具有一定的價(jià)值,有需要的朋友下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01jQuery實(shí)現(xiàn)可展開折疊的導(dǎo)航效果示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)可展開折疊的導(dǎo)航效果,結(jié)合實(shí)例形式分析了基于jquery.easing.1.3.js插件的展開折疊效果實(shí)現(xiàn)技巧,非常簡單實(shí)用,需要的朋友可以參考下2016-09-09jQuery實(shí)現(xiàn)顏色打字機(jī)的完整代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)顏色打字機(jī)的完整代碼,代碼簡單易懂,非常不錯(cuò),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03