-6

I have a textbox where I am calling a function with the 'onkeypress' event, but every time I call the function the page refresh, how can I do this in Ajax?

<input style="height:39px; margin-bottom:10px" onKeyPress="submitMe(event)" name="team" class="form-control" id="team" type="text">

Javascript function

function submitMe(event) 
{
        if (window.event.keyCode == 13)
        {
           navigateToLink('team', document.getElementById('team').value);
        }
}
Frank
  • 1
  • 1
  • `navigateToLink` does that I guess... – Rayon Mar 31 '16 at 07:56
  • 3
    You're not using ajax in any way here by the way. – Daan Mar 31 '16 at 08:00
  • What are you trying to do exactly? Just making a button do AJAX is pretty vague. – Gremash Mar 31 '16 at 08:00
  • navigateToLink is another function where I am passing the textbox value, everything works but it refresh the page everytime I enter a text in the textbox and press enter.. – Frank Mar 31 '16 at 08:00
  • have you already ajax included and just don´t show it to us? – el solo lobo Mar 31 '16 at 08:01
  • That isn't AJAX. AJAX is doing another web request to the server, getting some data back from the server, and then processing that data without refreshing. What you are doing is just plain javascript. What does navigateToLink do? – Gremash Mar 31 '16 at 08:02
  • No I don't have ajax, sorry for my english. I want to use ajax in the function – Frank Mar 31 '16 at 08:02
  • What are you trying to do. I don't think you understand what AJAX means. Can you include all of your code including the navigateToLink function? – Gremash Mar 31 '16 at 08:03
  • Your question is not clear: What must happen when a key got pressed inside the input field? Also, learn what AJAX does (`http://www.w3schools.com/ajax/default.asp`) – Ramon Bakker Mar 31 '16 at 08:06
  • When the enter key get pressed, the text entered by the user will be sent to the function 'navigateToLink', it's irrelevant what the 'navigateToLink' does, the thing is that every time I press enter in the textbox the web page will refresh, I thought using ajax will solve this – Frank Mar 31 '16 at 08:09
  • See related question: [How to prevent form from being submitted?](http://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted) – Yogi Mar 31 '16 at 08:14
  • 2
    No. We need more code as the page won't refresh unless your code tells it to. No on can help if you don't paste your entire HTML page and javascript. – Gremash Mar 31 '16 at 08:15

3 Answers3

2

Maybe because you sent the form html tag. Try to put your text input outside of it or make something like this :

<form onsubmit="return false;"></form>
DiD
  • 71
  • 3
0

See this plunkr ajax call on input.

I have created very basic call. I hope it will clear you within no time.

I used keyup event because it listen to action happens after entering values. So that we can get exactly whatever entered in the text box. Further you can do whatever with that input based on requirement.

Prasad Shinde
  • 658
  • 1
  • 7
  • 14
-1

You can try something like that :

$('input').on('keypress', function(event){
    inputValue = $(this).val();
    submitMe(event);
}
xxx
  • 1,153
  • 1
  • 11
  • 23
Joel
  • 167
  • 1
  • 1
  • 11