开发者

Problem in jquery enter key event

I need a little of your he开发者_开发知识库lp here.

In my case I am having search textbox on b.php, in which user can enter username and hit enter to get the serched user-details. Ok this is so far. Now my search code to deal with database is ready in c.php and i want to call it through b.php with jquery's event. actuaully my a.php is home file and calling b.php through it on click event. Now flow for jquery will be like this a.php > b.php > c.php

There is no button to search, hitting enter key only will give searched user.

I writ my code in jquery as:

$(document).ready(function(){

    $('#srchtxt').bind('click', function(){ 
    if($('#srchtxt').val() != '') {
              $('#loading').html('<img src="images/ajax-loader(1).gif">');
              $('#loading').show(); 
              $.get('/usersearch.php?tnm3='+arr3, '', function(data){
                $('#content').html(data);
                $('#loading').hide();    
        });     
            }   
   });  
   $('#srchtxt').bind('keyup', function(e){               
       if(e.keyCode==13) {
           $("#srchtxt").trigger('click');       
       }    
   });   
});

This event is not working. can you help me here?


I think you mixed search field and search button.

The proper solution is folowing:

$('#srchtxt').keyup(function(e){               
       if(e.keyCode==13) {
           $("#search-submit").trigger('click');       
       }
});
$('#search-submit').click(function(){alert('button has been clicked')});

See working example here


It is not working both on Firefox and IE.

For ie i think we have to use the following code to get the event

if(window.event) { window.event.keyCode }


Instead of .bind try to use .live() http://api.jquery.com/live/


I usually use event.which instead of event.keyCode It may be compatible with more browsers.

Plus, you mixed up some ID...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜