1. 程式人生 > >JS 實時監聽input的value值改變 解決方案

JS 實時監聽input的value值改變 解決方案

該方案在 FoxFire、Chrome、360急速、Microsoft Edge、IE11下測試均可以正常顯示。
html頁面程式碼:

  
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>測試頁面</title>
<link type="text/css" rel="stylesheet" href="../css/myhtml5.css"/>
</head>
<body>
<div><h1>
測試輸入框事件監聽</h1></div>
<div>
<table>
<tr><th><span>點選輸入框,輸入字元:</span></th></tr>
<tr><td><input id="input_text" type="text" placeholder="點選輸入" value=""/></td></tr>
<tr><td><input id="input_button"
type="button" value="點選結束" name="完成"/></td></tr>
</table>
</div>
<script src="../scripts/jq.js"></script>
<script src="../scripts/myhtml5.js"></script>
</body>

js程式碼:

  
$(document).ready(function () {

$("#input_text").bind("input propertychange",function
() {
$("#input_button").val("已經輸入了"+$("#input_text").val().length+"個字。。。");
});

$("#input_button").click(function () {
alert($("#input_text").val()+"\n"+$(this).val());
});
});