如何实现在表单中输入关键操作

时间:2013-12-01 05:21:17

标签: javascript jquery html

在我的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>

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

1 个答案:

答案 0 :(得分: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
}
}