Submit shouldn't blur textarea with jQuery?
I have a textarea with a blur function:
$("#comment").blur(function() {
...someth开发者_开发问答ing
});
I don't want this blur()
to happen when I click on the submit button below. How can I solve this?
Have you tried taking a look at When onblur occurs, how can I find out which element focus went *to*? -- it should help in this case so you can determine whether to fire the 'blur' function or not based on where the focus went to
a normal submit is a PAGE REFRESH
So: when your page loads again, the blur()
code will get called as well
In your submit pass a variable back to the server in the form that indicates that it was a form submit (ie a meaningful flag). When you re-render the page render it with the flag, stating it was a form submit, in javascript. Look for that flag in your blur handler, clear it, and return false.
Something like this should work:
var _commentBlurTimer = 0;
$("#comment").blur(function() {
_commentBlurTimer = window.setTimeout(function() {
//...something
}, 500);
});
$("input[type=submit]").click(function() {
if (_commentBlurTimer)
window.clearTimeout(_commentBlurTimer);
});
Test case: http://jsfiddle.net/yahavbr/a9xZW/
精彩评论