jQuery: call the action's form after calling preventDefault()
The code below shows an error message when a field is not filled.
As you can see I'm calling preventDefault(), but...how to call the action associated to the form if the field has开发者_C百科 been filled??
$('input#save').click(function(e){
e.preventDefault()
if($('#field_1').val() == ''){
$('#error_file').show();
}
});
$('input#save').click(function(e){
e.preventDefault()
if($('#field_1').val() == ''){
$('#error_file').show();
}
else
{
$('#my_form').submit();
}
});
The issue here is where you've placed e.preventDefault()
. You only want to prevent default if the validation fails, so just move it inside the if condition.
$('input#save').click(function(e){
if($('#field_1').val() == ''){
e.preventDefault();
$('#error_file').show();
}
});
精彩评论