如何使用键盘箭头键选择 li 下拉文本(无需鼠标点击)

huangapple go评论83阅读模式
英文:

How to select li dropdown text with keyboard arrow keys (without mouse click)

问题

我想要使用键盘箭头键从下拉列表中选择元素或行。请查看下面的截图:

enter image description here

如果你看到这张图片,在表格中有一列叫 Ledger Name。当我在输入时,下拉框弹出来了,但无法使用键盘箭头键选择它。

这些值来自数据库表,即通过 AJAX 调用 JSP 页面实现的搜索功能。

请在这方面帮助我。

以下是为此编写的 JQUERY 函数代码:
以下是用于 AJAX 调用和从数据库表中设置数据到 ul 的代码。

它是用 Jquery 和 AJAX 编写的。JSP 用于从数据库获取数据

$("#tbody").on("keyup", "input[name^=inputString]", function() {
  var $this = $(this);
  $("tbody tr").each(function() {
    var search = $(this).closest('tr').find("input").val();   
    if (search != '' && search != null) {
      $.ajax({
                  type: 'POST',
                  url: 'ledgers.jsp',
                  data: 'key=' + search,
                  success: function(data) {
                    $this.next('#showList').html(data);
                  }
      });
    } else {
          $('#showList').html('');
       }
    });
   });
      
   $("#tbody").on('click', 'li', function() {
     $(this).closest('tr').find("input").val($(this).text());
     $(this).closest('tr').find('#showList').html('');
   });
英文:

I wanted to select element or row from li dropdown with keyboard arrow keys. Please see the screenshot below:

enter image description here

If you see the image, there is column Ledger Name in table. When I am typing drodown is coming but not able to select it by keyboard arrow keys.

These values are coming from database table i.e. search functionality is implemented through AJAX call to JSP page.

Please help me on this.

Below is the JQUERY function code written for this :
Below is the code for AJAX call & setting data to ul from db table.

It is written in Jquery & AJAX. JSP is used to get data from database

$("#tbody").on("keyup", "input[name^=inputString]", function() {
var $this = $(this);
$("tbody tr").each(function() {
var search = $(this).closest('tr').find("input").val();   
if (search != '' && search != null) {
$.ajax({
type: 'POST',
url: 'ledgers.jsp',
data: 'key=' + search,
success: function(data) {
$this.next('#showList').html(data);
}
});
} else {
$('#showList').html('');
}
});
});
$("#tbody").on('click', 'li', function() {
$(this).closest('tr').find("input").val($(this).text());
$(this).closest('tr').find('#showList').html('');
});

答案1

得分: 1

下午好,

您可以查看datalist元素

这种方式已经内置了功能。

附带一些带有样式的示例


至于如何使用<li>来实现这一点:

  1. 在输入元素上添加一个onFocus事件监听器,执行步骤2
  2. 附加onKeyDown事件监听器以监听上/下/和回车键
  3. 然后您可以跟踪一个计数器,知道要突出显示哪个元素,向下箭头-> +1,向上箭头-1
  4. 确保0 < count < options.length,通过循环或夹值,取决于您的用例更合理。
  5. 按回车键会用所选元素替换输入元素中的值

无论如何,这只是一种实现此功能的方法,希望您会使用datalist,省去很多时间;)

英文:

Good afternoon,

You could take a look at the datalist element

That way the functionality is built in.

PS: here's some good examples with styles


As for how to accomplish this with &lt;li&gt;<b>

  1. have an event listener on you input element for onFocus that performs step 2
  2. attach the onKeyDown even listener to listen for up/down/and enter keys
  3. then you can keep track of a counter knowing what element to highlight, down-arrow -&gt; +1, up-arrow -1
  4. make sure that 0 &lt; count &lt; options.length, by either cycling or clamping, whichever makes more sense for your usecase.
  5. and pressing enter would replace the value in the input element with the selected element

Anyways, that is just one way to implement this feature, hopefully you'll just use the datalist and save yourself a lot of time 如何使用键盘箭头键选择 li 下拉文本(无需鼠标点击)

huangapple
  • 本文由 发表于 2023年3月9日 23:44:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定