【问题标题】:Want to execute function when press enter key on text field想要在文本字段上按回车键时执行功能
【发布时间】:2013-03-07 17:54:11
【问题描述】:

我有这个 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() 是函数:

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

当我在 PtName 文本中按回车键时,我想执行相同的 search_p() 函数 我该怎么做?

【问题讨论】:

    标签: ajax forms text


    【解决方案1】:

    在您的表单上指定onsubmit

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

    并将按钮的type 更改为submit

    <input name="Button" type="submit" id="button" value="Check" />
    

    【讨论】:

      【解决方案2】:

      您可以使用以下 jquery 函数来做到这一点:

      $("#PtName").keyup(function (e) {
          if (e.keyCode == 13) {
              // call function
          }
      });
      

      【讨论】:

        【解决方案3】:

        Javascript: 在javascript中输入以下函数

        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)">
        

        对于必需的文本字段输入onkeypress 事件

        【讨论】:

          【解决方案4】:

          在表单的提交事件上调用您的函数:-

          <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>
          

          【讨论】:

            【解决方案5】:

            我想要一个文本区域,它会在 shift+enter 上换行,并且在 Enter 上会提交: This seems to answer my query

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-01-17
              • 1970-01-01
              • 2014-05-12
              • 2022-08-19
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多