javascript Google Chrome 表单 onChange 未触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5124747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:02:48  来源:igfitidea点击:

Google Chrome form onChange not firing

javascripthtmlgoogle-chrome

提问by JoJo

How do I get Google Chrome (9.0.597.98 on Windows 7 64-bit) to fire a form's onchange? If you play around with the following example, nothing gets consoled out. It's working fine in Firefox 3.6.13.

如何让 Google Chrome(Windows 7 64 位上为 9.0.597.98)触发表单的 onchange?如果你玩下面的例子,什么都不会得到安慰。它在 Firefox 3.6.13 中运行良好。

<form onchange="console.info('form changed')">
 <select>
  <option>uno</option>
  <option>dos</option>
 </select>
 <input type="radio" name="videoDevice" value="tres" checked="checked" />
 <input type="radio" name="videoDevice" value="cuatro" checked="checked" />
</form>

回答by ggutenberg

Binding the changeevent via JS seems to work ok:

change通过 JS绑定事件似乎可以正常工作:

HTML

HTML

<form id="test">
 <select>
  <option>uno</option>
  <option>dos</option>
 </select>
 <input type="radio" name="videoDevice" value="tres" checked="checked" />
 <input type="radio" name="videoDevice" value="cuatro" checked="checked" />
</form>

JS

JS

document.getElementById('test').addEventListener('change', function() {
    alert('change fired');
}, false);

回答by inf3rno

Basically the form has only the following two events: onreset,onsubmit. You can do a workaround:

基本上表单只有以下两个事件:onreset,onsubmit。你可以做一个解决方法:

window.onload=(function ()
{
    var Form=
    {
        w3c: !!window.addEventListener,
        addEvent: function (form,type,listener)
        {
            var inputs=form.elements;
            for (var i=0,l=inputs.length; i<l; ++i)
            {
                var input=inputs[i];
                if (this.w3c)
                {
                    input.addEventListener(type,listener,false);
                }
                else
                {
                    input.attachEvent("on"+type,listener);
                }
            }
        },
        fixEvents: function (form)
        {
            var eventPattern=/^on(\w+)$/;
            var attribute;
            for (var i=0,l=form.attributes.length; i<l; ++i)
            {
                var attribute=form.attributes.item(i);
                var name=attribute.name;
                var value=attribute.value;
                if (eventPattern.test(name) && !form[name])
                {
                    var type=eventPattern.exec(name)[1];
                    var listener=new Function(value);
                    this.addEvent(form,type,listener);
                }
            }
        }
    };

    return function ()
    {
        var forms=document.getElementsByTagName("form");
        for (var i=0,l=forms.length; i<l; ++i)
        {
            Form.fixEvents(forms[i]);
        }
    };
})();

回答by Ogun Mericligil

     var button = document.form.adet;
        if (button.addEventListener) {  // all browsers except IE before version 9
            button.addEventListener ("change", function () { alert('1'); }, false);
        }
        else {
            if (button.attachEvent) {   // IE before version 9
                button.attachEvent ("onchange", function () { alert('2'); });
            }
        }