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

