简体   繁体   English

我想使用 JavaScript 到 select 输入,当我将选项卡聚焦在按钮元素上并按“Enter”时。

[英]I want to use JavaScript to select an input when I'm tab focused on a button element and press “Enter.”

New dev here trying to get comfortable with JavaScript/jQuery.这里的新开发人员试图熟悉 JavaScript/jQuery。

I'm currently developing a page with tabs that shuffles content on my page based on which tab (button) is selected.我目前正在开发一个带有选项卡的页面,该选项卡根据选择的选项卡(按钮)在我的页面上随机播放内容。 The tabs are based on this code: https://codepen.io/broskibro/pen/VwKRmKQ选项卡基于以下代码: https://codepen.io/broskibro/pen/VwKRmKQ

  <input type="radio" id="tab1" name="tab-control" checked>
  <input type="radio" id="tab2" name="tab-control">
  <ul>
    <li id="list1" title="Job Seeker"><label tabindex="0" class="btn lg" for="tab1" role="button">
      <br><span>Job Seeker</span></label></li>
    <li id="list2" title="Employer"><label tabindex="0" class="btn lg" for="tab2" role="button">
        <br><span>Employer</span></label></li>
  </ul>

I've gotten all the functionality that I need from it, but the outstanding issue is that I need the feature to be accessible.我已经从中获得了我需要的所有功能,但突出的问题是我需要该功能是可访问的。 It's easy enough to add tab-index=0 to the buttons so that they can be reached via tabbing, but the problem is that the input is what needs to be tab-indexed to and "Enter-ed" to actually activate the functionality.tab-index=0添加到按钮很容易,以便可以通过 tab 键访问它们,但问题是input需要被 tab 索引和“Enter-ed”才能实际激活功能。 I want to make it so that I can use JS so that when I'm focused on the buttons and I press enter, the respective is activated.我想这样做,以便我可以使用 JS,以便当我专注于按钮并按 enter 时,相应的被激活。 I'm assuming it will be something using the keydown feature.我假设这将是使用keydown功能的东西。 I'm repurposing some code I got to work for another project I was working on, but I imagine it would look something like:我正在重新利用一些代码,以便为我正在从事的另一个项目工作,但我想它看起来像:

var input = document.getElementById("list1")
input.addEventListener("keydown", function(event) {
    if (event.key == 'Enter') {
        // Trigger the click for the input     
        document.getElementById("tab1").click();
    }
});

It looks like it should be a relatively easy solution but I'm getting a variety of different errors.看起来它应该是一个相对简单的解决方案,但我遇到了各种不同的错误。 Any guidance is appreciated!任何指导表示赞赏!

I can't get tabbing to work in the codepen example, so its difficult to test the solution.我无法在 codepen 示例中使用选项卡,因此很难测试解决方案。 But it seems like you are on the right track, I wrote up this:但看起来你在正确的轨道上,我写了这个:

var input = document.getElementsByTagName("input");
for (var i = 0; i < input.length; i++) {
    input[i].addEventListener("keydown", function(event) {
        if (event.key == 'Enter') {
            // Trigger the click for the input 
            var clickEvent = new MouseEvent("click", {
                "view": window,
                "bubbles": true,
                "cancelable": false
            });
    
            document.getElementById("tab" + i).dispatchEvent(clickEvent);
        }
    });
}

So it turns out that my major issue was that I was adding IDs to the wrong HTML elements.所以事实证明,我的主要问题是我向错误的 HTML 元素添加了 ID。 After adjusting the HTML, the code below works.调整 HTML 后,下面的代码工作。 Next, I'll look into properly setting up a loop so that I can reduce the size of my function.接下来,我将研究如何正确设置循环,以便减小 function 的大小。

<input type="radio" id="tab1" name="tab-control" checked>
  <input type="radio" id="tab2" name="tab-control">
  <ul>
    <li title="Job Seeker"><label id="slide1" tabindex="0" class="btn lg" for="tab1" role="button">
      <br><span>Job Seeker</span></label></li>
    <li title="Employer"><label id="slide2" tabindex="0" class="btn lg" for="tab2" role="button">
        <br><span>Employer</span></label></li>
  </ul>
document.addEventListener( 'DOMContentLoaded', function() {
    const list1 = document.getElementById("slide1");
    const output1 = document.querySelector("#tab1");
    const list2 = document.getElementById("slide2");
    const output2 = document.querySelector("#tab2");

    list1.addEventListener("keydown", function(event) {
        if (event.key == 'Enter') {
            // Trigger the click for the input
            output1.click();
        }
    });

    list2.addEventListener("keydown", function(event) {
        if (event.key == 'Enter') {
            // Trigger the click for the input
            output2.click();
        }
    });
});

暂无
暂无

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

相关问题 通过按Enter键触发绑定到HTML按钮的JavaScript函数。 (不使用Tab键) - Triggering JavaScript function tied to an HTML button by pressing Enter. (Without using Tab key) 当我在输入文本框中输入任何搜索关键字然后按Enter时,阻止表单提交。 使用jQuery - Prevent form submit when I put any search keyword on the input text box then pressed enter. Using Jquery 当我按Enter键时引用我所在的文本字段 - Reference the text field that I'm in when I press enter 单击或输入后,如何获取搜索栏中的信息。 然后将其粘贴到另一页上的另一个输入中? - How can I get the Information in search bar on click or Enter. Then paste it to another input on another page? 我的输入字段是听到每个“keydown”事件但是输入。 我有一个日期选择器,我不确定最新情况 - My input field is hearing every “keydown” event but the enter. I have a datepicker on it and am unsure whats going on 当我在不同的文本输入上按回车键时,如何使表单提交不同的按钮? - How to make form submit different button when I press enter on different text input? 我想在输入聚焦时更改样式在 React 中 - I want to change the style when the input is focused In React 当我按下获取路线按钮时,我希望我的javascript变量存储在数据库中 - when i press get route button i want my javascript variable to get stored in a database 当我按下回车键时如何触发网站的按钮? - how to trigger the button of the website when i press the enter key? 如何仅在用户按下 Enter 按钮时进行搜索? - How do I search only when user press enter button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM