English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Manual de Referência HTML

大全 de etiquetas HTML

Propriedade onchange do HTML

onchange pode ser usado para obter ou definir a função de manipulação de eventos do evento change do elemento atual.

HTML Event Attributes

Exemplo Online

Quando o usuário alterar a opção selecionada do elemento <select>, execute o JavaScript:

<!DOCTYPE html>
<html>
<head>
<title>Uso da propriedade onchange do HTML (Tutorial Básico da Web oldtoolbag.com)</title>
</head>
<body>
<p>Selecione um novo carro da lista.</p>
<select id="mySelect" onchange="myFunction()">
  <option value="Audi">Audi
  <option value="BMW">BMW
  <option value="Mercedes">Mercedes
  <option value="Volvo">Volvo
</select>
<p>Quando você selecionar um novo carro, uma função é acionada que exibe o valor do carro selecionado.</p>
<p id="demo"></p>
<script>
function myFunction() {
  var x = document.getElementById("mySelect").value;
  document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>
</body>
</html>
Test and see ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

All major browsers support the onchange event attribute

Definition and Usage

The onchange attribute triggers when the value of the element changes.

Hint: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of the element changes, while the onchange event occurs when the element loses focus. Another difference is that the onchange event also applies to <select> elements.

HTML 4.01 and HTML5The differences between

No difference.

Syntax

<element onchange="script">

Attribute Value

ValueDescription
script指定在onchange事件触发时执行的脚本。
HTML Event Attributes