2016-02-18 56 views
0

我正在构建一个多选题测验游戏,我试图模仿Duolingo的功能,其中基本上你有3个编号的多选答案,用户只需键入“1”,“2”或“3”,以选择他们喜欢的答案选项(见图片)。基于键盘上的用户号码选择选择多选答案

如何模仿此功能?使用JS或其他工具。目前,我的应用程序中的每个答案选项都有一个隐藏的无线电输入,用户必须用鼠标点击答案才能选择,但鼠标是最差的!任何帮助将是伟大的!谢谢!

duolingo answer selection example

回答

1

如果你的表格是HTML表单是这样的:

<form> 
<input type="radio" name="gender" id="button1" value="red" checked>Red<br> 
<input type="radio" name="gender" id="button2" value="blue"> Blue<br> 
<input type="radio" name="gender" id="button3" value="green"> Green 
</form> 

然后你只需要其中单选按钮被选中

$(document).keypress(function(e) { 
    console.log("keypress noticed"); 
if(e.which == 49) { 
     $("#button1").prop("checked", true) 

} 
    if(e.which == 50) { 
     $("#button2").prop("checked", true) 
} 
    if(e.which == 51) { 
     $("#button3").prop("checked", true) 
} 
}); 
0

猜测可能在某种程度上,监听键盘输入,如果钥匙进入,做X

Simplest way to detect keypresses in javascript,在情况下可能会感兴趣

也:

$(document).on('keypress', function(e) { 
    console.log(String.fromCharCode(e.keyCode)); 
}) 

万一可能感兴趣

+0

如何检查,如果用户输入“1”或“2”,“3”等jQuery来改变一下呢?出于某种原因,我只能找到一般按键的信息,例如左/右/输入。 – strohy1210

+0

回复修改!另外,如果我不得不猜测,使用vanilla js技术的关键值将在'keyCode'上,并且在'哪个'上可能有兴趣使用jQuery方法 – Drew

1

使用mousetrap!它捕获页面上的按键,并允许您为每个键设置事件处理程序。为每个需要的数字键分配一个处理程序将是一个简单的练习。

+0

正是我在找的 - 谢谢! – strohy1210

+0

不客气。 –

0
$("input").on("keydown", function (event) 
    { 
     if (event.which == 13) 
     { 
      event.preventDefault(); 
      .. 
     } 
    }); 

13是进入不知道至极一个是1,2,3只是CONSOLE.LOG(event.wich)