submitting value of textarea on enter key
Right now I have this flow in jquery:
$('input#search').click(function(){
$('textarea#people').fadeIn();
$('input#searchpeople').fadeIn();
$('input#add').hide();
$('input#search').hide();
});
$("#searchpeople").click(function(){
var people=$("textarea#people").val();
$.post("searchpeople.php",{people:people},functio开发者_开发技巧n(result){
$("div.callback").html(result);
});
});
My simple question is, instead of having the user having to click the search button, the button would automatically click when the user presses the enter key.
You have to do the following...
- On keypress of textbox, check for 'Enter Key'(Key code of enter is 13)
- If so, call button click function
Demo can be found here
$('input#search').click(function(){
$('textarea#people').fadeIn();
$('input#searchpeople').fadeIn();
$('input#add').hide();
$('input#search').hide();
});
$('input#search').keypress(function(){
if($(this).keyCode == 13)
{
Search();
}
});
$("#searchpeople").click(Search);
function Search()
{
var people=$("textarea#people").val();
$.post("searchpeople.php",{people:people},function(result){
$("div.callback").html(result);
}
Use keydown event instead of click event: http://api.jquery.com/keydown/
Instead of handling clicks and key presses, you can just use a form
and handle the submit
event:
<form>
<textarea id="people"></textarea>
<input id="search" type="submit" />
</form>
<script>
$('form').submit(function () {
// do stuff
return false;
});
</script>
精彩评论