0

I have thi HTML:

<form id="form1" name="form1" method="post" action="">
  <input name="PtName"   type="text" id="PtName" />
  <input name="Button" type="button" id="button"  onclick="search_p()" value="Check" />
        </form>

serach_p() is function:

<script type="text/javascript">
function search_p(){
         $.ajax({
      url: 'srchpt.php',
      type: 'POST',
      data: { PtName: $('#PtName').val()},
      success: function(data){
        $(".myresult").html(data);
      }
    })
}
</script>

I want when I press enter key in PtName text do same search_p() function How can I do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
sermed
  • 105
  • 3
  • 11

5 Answers5

1

Specify an onsubmit on your form:

<form ... onsubmit="search_p(); return false">

and change the type of your button to submit:

<input name="Button" type="submit" id="button" value="Check" />
techfoobar
  • 65,616
  • 14
  • 114
  • 135
0

you can do this by using following jquery function:

$("#PtName").keyup(function (e) {
    if (e.keyCode == 13) {
        // call function
    }
});
I_Debug_Everything
  • 3,776
  • 4
  • 36
  • 48
0

Javascript : In javascript put the following function

function enterPressed(event) {
    var key;

    if (window.event) {
        key = window.event.keyCode; //IE
    } else {
        key = event.which; //firefox
    }

    if (key == 13) {
        yourFunction();
       // do whatever you want after enter pressed event. I have called a javascript function
    }
}

HTML :

<input type="text" onkeypress="javascript:enterPressed(event)">

For required textfield put onkeypress event

Sagar Dalvi
  • 200
  • 1
  • 9
0

Call your function on submit event of your form:-

<html>
    <head>
        <script type="text/javascript">
            function search_p(){
                $.ajax({
                    url: 'srchpt.php',
                    type: 'POST',
                    data: { PtName: $('#PtName').val()},
                    success: function(data){
                        $(".myresult").html(data);
                    }
                })
            }
        </script>
    </head>
    <body>
        <form id="form1" name="form1" method="post" action="" onsubmit="search_p()" >
            <input name="PtName"   type="text" id="PtName" />
            <input name="Button" type="button" id="button"  onclick="search_p()" value="Check" />
        </form>
    </body> 
</html>
Nitin Kumar Soni
  • 198
  • 2
  • 16
0

I wanted a textarea that would break-line on shift+enter, and on Enter would submit: This seems to answer my query

Alia Anis
  • 1,355
  • 11
  • 7