L'evento "onchange" viene attivato solo quando USER inserisce un valore. Perché non è possibile attivare l'evento quando cambio il valore automaticamente tramite Javascript? C'è un'alternativa?Evento "onchange" di trigger
Animazione:
Codice:
<!DOCTYPE html>
<html>
<head>
<script>
document.addEventListener ("DOMContentLoaded", function() {
var input = this.getElementsByTagName ("input")[0];
var div = this.getElementsByTagName ("div")[0];
var i = 0;
var seconds = 5;
div.innerHTML = "The following input should fire the event in " + seconds + " seconds";
var interval = window.setInterval (function() {
i ++;
if (i === seconds) {
window.clearInterval (interval);
input.value = "Another example";
div.innerHTML = "Nothing ! Now try change the value manually";
}
else {
div.innerHTML = "The following input should fire the event in " + (seconds - i) + " seconds";
}
}, 1000);
input.addEventListener ("change", function() {
alert ("It works !");
}, false);
}, false);
</script>
<style>
body {
padding: 10px;
}
div {
font-weight: bold;
margin-bottom: 10px;
}
input {
border: 1px solid black;
border-radius: 3px;
padding: 3px;
}
</style>
<title>Event</title>
</head>
<body>
<div></div>
<input type = "text" value = "Example" />
</body>
</html>
Grazie
È necessario attivare manualmente l'evento dopo aver modificato il valore. Vedi questa domanda: http://stackoverflow.com/q/2490825/615754 – nnnnnn
quando si modifica il valore di input con javascript perché non si attiva anche qualche evento? perché vuoi usare onchange? l'onchange è per l'input dell'utente il resto che puoi gestire da solo. – ggzone