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