[英]How to open jQuery-UI-Selectmenu dropdown using 'ENTER' key using custom logic? Documentation only allows 'SPACE' key to open but not 'Enter' key
我正在尝试将 jQuery-UI-Selectmenu 用于下拉功能。
根据https://api.jqueryui.com/selectmenu/上为选择菜单提供的 API 文档,“SPACE”键可用于打开选择菜单,但不能用于“ENTER”。
可以使用“ENTER”键打开基本的 HTML 选择,但不能使用选择菜单。 对于可访问性要求,我还需要使用“ENTER”键打开选择菜单。
能否请你帮忙。 下面是我用来创建下拉菜单的代码。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Nuber</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#number" )
.selectmenu()
.selectmenu( "menuWidget" )
.addClass( "overflow" );
} );
</script>
</head>
<body>
<div class="demo">
<form action="#">
<select name="number" id="number">
<option>1</option>
<option >2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
</form>
</div>
</body>
</html>
这有点hacky,但你可以做这样的事情(可能必须在第一次点击输入两次):
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Nuber</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-3.5.1.js"></script> <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script> <script> $(function() { let isOpen = false $(".container").on("keydown", function(event) { if (event.which === 13) { isOpen = !isOpen if (!isOpen) { $("#number").selectmenu("open"); } else { $("#number").selectmenu("close"); } } }) $("#number").selectmenu() .selectmenu("menuWidget") .addClass("overflow"); }); </script> </head> <body> <div class="container"> <form action="#"> <div id="foo"> <select name="number" id="number"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> </select> </div> </form> </div> </body> </html>
我希望这有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.