I need to keep the focus on the text input when the user moves to the next input without put开发者_如何学JAVAting something in the previous one. In short, if it is null (left blank) or not valid, how do I keep the focus on that input until the conditions are satisfied?
$("#inputID").blur(function(){
if ("#inputID").val() == "")
{
$("#inputID").focus();
}
});
Code done this way for the concept of explaining that is always referring to the same input. In practice 'this' would be a better option.
$( element ).blur( function( e ) {
var ev = e || event;
if( $( this ).val() == '' ) {
$( this ).focus();
return false;
}
});
You don't really need jquery for it
<input type="text" onblur="if (!this.value) this.focus()">
Although, I don't think that it's a good idea. If it's a form, then it's better to show the list of required fields when the user tries to submit it.
精彩评论