【问题标题】:How can implement enter key action in a form如何在表单中实现输入键操作
【发布时间】:2013-12-01 05:21:17
【问题描述】:

在我的 php 应用程序中,我使用文本字段和按钮[搜索]。

当我输入一些数据并按 Enter 键时,按钮操作不起作用。

有人可以帮忙吗?

我的代码喜欢,

    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    <script>
    function clearerrormessage()
         {  
        document.getElementById('info').innerHTML="Enter a keyword to find prospective customer";

         }
    function submi()
      {
      /*$('#keyword').keyup(function(e) {
          if (e.keyCode == 13) {
           $('#form1').submit();
         }
       });*/
 keyword1 =document.getElementById('keyword').value;

    //var unvalidkey = new Array("is","am","a","b",'',"are","was","were");
  var unvalidkey = new Array("is","am","a","b",'',"are","was","were","+","/","%",".","&","\\","\"","'","?","#");
for(keyitem in unvalidkey) 
{
    if(unvalidkey[keyitem]==keyword1)
    var f=0;
}  
if(f==0)
{
    document.getElementById('info').innerHTML = '<h3><font color = red>Please enter a valid keyword to search</font></h3';
    return false;
}

  keyword=keyword1.replace(' ','_');
  $.post( "<?php echo base_url() ?>ajax/followsearch.php", { keyword  : keyword })
  .done(function( data ) {
  $("#content1").html("<div id ='content'></div>");
  $('#content').scrollPagination(

  { 

nop     : 30, // The number of posts per scroll to be loaded
offset  : 0, // Initial offset, begins at 0 in this case
error   : 'No Results To Display!', // When the user reaches the end this is the message that is
                            // displayed. You can change this if you want.
delay   : 500, // When you scroll down the posts will load after a delayed amount of time.
               // This is mainly for usability concerns. You can alter this as you see fit
scroll  : true // The main bit, if set to false posts will not load as the user scrolls. 
               // but will still load if the user clicks.
  }
        );

  });

  }

    </script>
    </head>
    <body>
    <form id="form1" name="form1" action="" />
    <div id="followkeyword">
    <input type="text" style="width:300px;height:26px" name="keyword" id="keyword"  onClick="clearerrormessage()" />
    <div id="info1"><span id="info">Enter a keyword to find prospective leads</span>                                
    </div>                    
   </div>   
   <div id="followperloc">

   <input type="button" name="mysubmit" id="mysubmit" value="Display my prospective leads" onclick="return submi()"/>           
   </div>
   </form>

   </body>
   </html>

此代码在按钮单击中正常工作。但我还需要输入密钥提交。请任何人帮助我..

【问题讨论】:

  • 为什么你的函数叫submi()?这看起来很古怪

标签: javascript jquery html


【解决方案1】:

检查事件键码 13 以检测回车键。试试这个:

Html:使用onkeypress 代替onclick,因为onclick 是一个鼠标事件。

<input type="button" name="mysubmit" id="mysubmit" value="Display my prospective leads"
 onkeypress="return submi(event)" onclick="return submi(event)"/>  

javascript:

function submi(event) {
event.which = event.which || event.keyCode;
if(event.which == 13 || event.which == 1) {
    // wrap up your entire code here
}
}

【讨论】:

  • 使用onkeypress 代替onclick 因为onclick 是一个鼠标事件。检查我更新的答案..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多