HTML事件屬性onchange
HTML事件屬性onchange
觸發onchange
屬性事件當元素的值改變時。
HTML5中的新功能
沒有。
句法
<element onchange="script or Javascript function name">
支持的標簽
<input type="checkbox">, <input type="file">, <input type="password">, <input type="radio">, <input type="range">, <input type="search">, <input type="text">, <keygen>, <select> <textarea>
瀏覽器兼容性
onchange | Yes | Yes | Yes | Yes | Yes |
例子
<!DOCTYPE html>
<html>
<body>
<select id="mySelect" onchange="myFunction()">
<option value="CSS">CSS</option>
<option value="HTML">HTML</option>
<option value="Javascript">Javascript</option>
<option value="SQL">SQL</option>
</select>
<script>
function myFunction() {
alert(document.getElementById("mySelect").value);
}
</script>
</body>
</html>
Click to view the demo
實施例2
<!DOCTYPE html>
<html>
<body>
Enter some text:
<input type="text" name="txt" value="Hello" onchange="myFunction(this.value)">
<input type="text" name="txt2" value="Hello another" onchange="myFunction(this.value)">
<script>
function myFunction(val) {
alert(val);
}
</script>
<p>Edit text and tab out.</p>
</body>
</html>
Click to view the demo