jquery - enter key behaves differently depending on focus?
Is there a way (using jQuery) to have the enter behave differently depending upon what textfield is currently active?
So, if I have:
<input type="text" name="different" value="something" />
<button id="somebut开发者_开发技巧ton">Go</button>
<textarea name="something">Some stuff</textarea>
<input type="submit" name="sendit" value="Send It" />
I'm trying to figure out how to run a function if the user is in the and either hits enter or clicks , whereas if the user is in the textarea and hits enter, it'll submit the form...
Sorry, I'm not entirely certain of what to call this kind of behavior, which has made googling for it difficult...
$("input[name=different]").keyup(function(e){
if(e.keyCode == '13')
{
e.preventDefault();
}
});
$("textarea[name=something]").keyup(function(e){
if(e.keyCode == '13')
{
$("form").submit();
}
});
jsFiddle
You would need to attach a function to that textbox action and submit the form with JQuery.
Use IDs instead of names on your html elements. Then do the following in javascript:
$("#different").keyup(function(e){
if (e.keyCode == 13) {
//do something
}
});
$("#something").keyup(function(e){
if (e.keyCode == 13) {
//do something
}
});
Here is an implementation on jsFiddle: http://jsfiddle.net/ByvjV/
精彩评论