开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜