开发者

jQuery: call the action's form after calling preventDefault()

开发者 https://www.devze.com 2023-03-25 06:33 出处:网络
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 f

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();
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消