2016-02-18 95 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)