jQuery/JS監(jiān)聽input輸入框值變化實例
input事件:
onchange:
1、要在 input 失去焦點的時候才會觸發(fā);
2、在輸入框內(nèi)容變化的時候不會觸發(fā)change,當(dāng)鼠標(biāo)在其他地方點一下才會觸發(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、在用戶輸入時觸發(fā),它是在元素值發(fā)生變化時立即觸發(fā);
2、該事件在 <input> 或 <textarea> 元素的值發(fā)生改變時觸發(fā)。
3、缺陷:從腳本中修改值不會觸發(fā)事件。從瀏覽器下拉提示框里選取值時不會觸發(fā)。IE9 以下不支持,所以IE9以下可用onpropertychange 事件代替。
JS: <input type="text" id="password" oninput="change()">
jQuery: $("#password").on('input propertychange', change);
onpropertychange:
1、會實時觸發(fā),會在元素的屬性改變時就觸發(fā)事件。當(dāng)元素disable=true時不會觸發(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('在實時觸發(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)知識點,感謝大家的學(xué)習(xí)和對腳本之家的支持。
相關(guān)文章
JS 遍歷 json 和 JQuery 遍歷json操作完整示例
這篇文章主要介紹了JS 遍歷 json 和 JQuery 遍歷json操作,結(jié)合完整實例形式詳細分析了JavaScript與jQuery遍歷json格式數(shù)據(jù)的簡單實現(xiàn)技巧,需要的朋友可以參考下2019-11-11
使用prop解決一個checkbox選中后再次選中失效的問題
下面小編就為大家?guī)硪黄褂胮rop解決一個checkbox選中后再次選中失效的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
jQuery插件formValidator實現(xiàn)表單驗證
這篇文章主要為大家詳細介紹了jQuery插件formValidator實現(xiàn)表單驗證的相關(guān)資料,需要的朋友可以參考下2016-05-05
JQuery通過AJAX從后臺獲取信息顯示在表格上并支持行選中
這篇文章主要介紹了JQuery通過AJAX從后臺獲取信息顯示在表格上并支持行選中的相關(guān)資料,需要的朋友可以參考下2015-09-09
Spring MVC中Ajax實現(xiàn)二級聯(lián)動的簡單實例
下面小編就為大家?guī)硪黄猄pring MVC中Ajax實現(xiàn)二級聯(lián)動的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07

