简体   繁体   English

为什么onchange事件不起作用

[英]why onchange event doesn't work

Why the onchange event doesn't work? 为什么onchange事件不起作用? below is my code: The php file is normally now problem. 下面是我的代码:php文件现在通常是问题。 because when i navigate to an other number with de input tag only, it works. 因为当我导航到仅带有de input标签的其他数字时,它可以工作。 But when i navigate with the four buttons it doesn't work. 但是当我用四个按钮导航时,它不起作用。 thank you for response! 谢谢您的回应!

    <script>
function splus() {
    document.getElementById("scene").value++;
    document.getElementById("take").value="1";
}
function smin() {
    if(document.getElementById('scene').value >1){
    document.getElementById("scene").value--;}
}
function tplus() {
    document.getElementById("take").value++;
    document.getElementById('comment').value = '';
}
function tmin() {
    if(document.getElementById('take').value >1){
    document.getElementById("take").value--;}
}
function change(scene,take) {
     if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("comment").innerHTML = xmlhttp.responseText;
            }
        };

        xmlhttp.open("GET","selectst.php?scene="+scene+"&take="+take,true);
        xmlhttp.send();
}
</script>
<body>
<button onclick="splus()" id="scene+">scene+</button>
<button onclick="smin()" id="scene-">scene-</button>
<button onclick="tplus()" id="take+">take+</button>
<button onclick="tmin()" id="take-">take-</button>
<input id="scene" type="number" onchange="change(this.value,document.getElementById('take').value)" value="1">
<input id="take" type="number" onchange="change(document.getElementById('scene').value,this.value)" value="1">
<textarea id="comment" rows="4" cols="50" placeholder="comments..."></textarea>
</body>

This is because change event is fired only if there was an interaction with the element. 这是因为仅当与元素发生交互时才会触发change事件。 If you change element's value from javascript, it does not fires it and you will have to fire change event manually. 如果您从javascript更改元素的值,则不会触发它,您将不得不手动触发change事件。 You can refer following [MDN - post]/( https://developer.mozilla.org/en-US/docs/Web/Events/change ) for reference 您可以参考以下[MDN-post] /(https://developer.mozilla.org/en-US/docs/Web/Events/change )以供参考

Here's a workaround. 这是一种解决方法。 I am using onfocus(). 我正在使用onfocus()。

 function splus() { document.getElementById("scene").value++; document.getElementById("take").value = "1"; document.getElementById("scene").focus(); } function smin() { if (document.getElementById('scene').value > 1) { document.getElementById("scene").value--; } document.getElementById("scene").focus(); } function tplus() { document.getElementById("take").value++; document.getElementById('comment').value = ''; document.getElementById("take").focus(); } function tmin() { if (document.getElementById('take').value > 1) { document.getElementById("take").value--; } document.getElementById("take").focus(); } function change(scene, take) { alert("Clicked"); if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("comment").innerHTML = xmlhttp.responseText; } }; xmlhttp.open("GET", "selectst.php?scene=" + scene + "&take=" + take, true); xmlhttp.send(); } 
 <button onclick="splus()" id="scene+">scene+</button> <button onclick="smin()" id="scene-">scene-</button> <button onclick="tplus()" id="take+">take+</button> <button onclick="tmin()" id="take-">take-</button> <input id="scene" type="number" onfocus="change(this.value,document.getElementById('take').value)" value="1"> <input id="take" type="number" onfocus="change(document.getElementById('scene').value,this.value)" value="1"> <textarea id="comment" rows="4" cols="50" placeholder="comments..."></textarea> 

With this solution : https://stackoverflow.com/a/2856602/5546267 使用此解决方案: https : //stackoverflow.com/a/2856602/5546267

 function tplus() { document.getElementById("take").value++; if ("createEvent" in document) { var evt = document.createEvent("HTMLEvents"); evt.initEvent("change", false, true); document.getElementById("take").dispatchEvent(evt); } else document.getElementById("take").fireEvent("onchange"); } 
 <input onchange="alert('change !')" id="take" value="1"><br> <button onclick="tplus()">run</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM