简体   繁体   English

使用键盘快捷键检查的无线电输入不会触发事件更改

[英]Radio input checked with keyboard shortcut doesn't trigger event change

I have added a keyboard shortcut to my script so that when I press ctrl + 1 the first "source" radio input is checked : 我在脚本中添加了键盘快捷键,以便在按ctrl + 1检查第一个“源”单选输入:

    if (e.key == "1" && e.ctrlKey) {
        document.getElementsByName("source")[0].checked = true;

    }

So far everythin is good. 到目前为止,一切都很好。 I have added an event listener so that when the state of my radio input are changed, then several things happen. 我添加了一个事件侦听器,以便在更改单选输入的状态时发生几件事。 But this event listener isn't triggered when I press ctrl+1 even thought the state of the radio input is changed (I can see the color of the input changing). 但是,即使我以为单选输入的状态已更改(我可以看到输入的颜色已更改)时,按ctrl+1也不触发此事件侦听器。 If click manually on the radio input then the event listener is working: 如果手动单击单选输入,则事件侦听器正在运行:

var radiosource = document.getElementsByName("source");
for (var i = 0; i < radiosource.length; i++) {
    radiosource[i].addEventListener('change', function (e) {
        var input_changed_id = e.target.id;
        if (input_changed_id.includes("en")) {
            document.getElementById("fr_target").checked = true;
            current_target = "fr";
        }
        if (input_changed_id.includes("fr")) {
            document.getElementById("en_target").checked = true;
            current_target = "en";
        }
        translate();
    });
}

Here is the full script (cf lines 119 and lines 49 and 322) 这是完整的脚本(请参见119行以及49和322行)

It won't trigger if you change the checked value manually but your event will trigger if you simulate the click on the radio button using .click() . 如果您手动更改检查值,则不会触发,但如果使用.click()模拟单选按钮的单击,则事件将触发。

So just update your keyboard shortcut script to this: 因此,只需将您的键盘快捷方式脚本更新为:

if (e.key == "1" && e.ctrlKey) {
  document.getElementsByName("source")[0].click();
}

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

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