answer questions using keyborad letters as short cut

时间:2018-03-25 19:53:25

标签: javascript jquery html jquery-ui

Can we answer questions using keyborad letters as short cut. In other words, if we have a Choice question like Gender and choices are Male/Female. Can this question answered by clicking on M/F or 1/2?

Html Code:

 <div class="">
        <strong class=""><span style="">Gander</span></strong>
        <input  type="radio" name="Gander_sq_101" value="001Male" > Male
        <input  type="radio" name="Gander_sq_101" value="002Female"> Female
 </div>

enter image description here

3 个答案:

答案 0 :(得分:0)

You can add javascript to handle keypress or keydown event.

document.onkeypress = function(e){console.log(e)};

Then switch e.which or e.key param and if it is needed key, execute needed functionality.

答案 1 :(得分:0)

This is how you do that using jquery

$(document).on('keyup' , function(e){
  var MaleOrFemale = (e.which == 49 || e.which == 77)? '001Male' : (e.which == 50 || e.which == 70) ? '002Female' : '';
  $('[name="Gander_sq_101"][value="'+MaleOrFemale+'"]').prop('checked' , true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="">
      <strong class=""><span style="">Gander</span></strong>
      <input type="radio" name="Gander_sq_101" value="001Male" > Male
      <input  type="radio" name="Gander_sq_101" value="002Female"> Female
</div>

Note : after click Run Code Snippet you need to focus on iframe to see the result

答案 2 :(得分:0)

You can check it with jQuery:

$( "body" ).keypress(function(e) {
  console.log(e);
});

Then you need to check e.keyCode for the key you want. Here you can simply find your right keycode: http://keycode.info