Submit when enter is pressed on number input

时间:2016-04-25 08:59:36

标签: javascript html input numbers

I want to make it so when I press enter when clicked on this number input box

<input id="answer" type="number" style="display: none;" placeholder="Answer Box"/>

it runs this function

NextQuestion()

I want it so when I press enter it runs the function

3 个答案:

答案 0 :(得分:1)

You have to bind on keyUp on this input and check if the keyCode is enter (13). In jQuery it will be :

$("input#answer").on("keyup",function(e){
    if(e.which==13)
       NextQuestion();
});

答案 1 :(得分:1)

Use can use .keypress() and .click() function in Jquery to bind multiple events to same function

$('#answer').keypress(function(e){
    if(e.which == 13)
        //Enter key code is 13, this will capture when enter key pressed
        NextQuestion();
});

$('#answer').click(function(e){
    NextQuestion();
});

答案 2 :(得分:0)

You can use keyup event listener

var doc = document.getElementById("answer");
doc.addEventListener("keyup", function(e){
  if(e.which==13) {
    alert('Enter key pressed, move to next question')
    //NextQuestion();
  }
});
<input id="answer" type="number" style="" placeholder="Answer Box" />