Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery submit vs. javascript submit

I am trying to invoke a form validation when clicked on ordinary button. This seems to work good with jQuery, but not with plain javascript. Can anyone explain this difference between jquery's .submit() and javascript's .submit() methods? Or what am I doing wrong?

<html>
<head>
<title>Form Submit</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">
function validateForm(form) {
  if (form.username.value=='') {
    alert('Username missing');
    return false;
  }
  return true;
}
</script>

</head>

<body>

<form action="index.php" name="loginform" method="post" onsubmit="return validateForm(this);">
  <input name="username" placeholder="username" required="required" type="text" />
  <input name="send1" type="button" value="Login1" onclick="$(document.forms.loginform).submit();" />
  <input name="send2" type="button" value="Login2" onclick="document.forms.loginform.submit();" />
  <input name="send3" type="submit" value="Login3" />
  <a href="" onclick="event.preventDefault(); $(document.forms.loginform).submit();"> Login4 </a>
  <a href="" onclick="event.preventDefault(); document.forms.loginform.submit();"> Login5 </a>
</form>

</body>
</html>

onsubmit results

like image 737
Stano Avatar asked Jul 19 '12 09:07

Stano


People also ask

Is jQuery submit deprecated?

No, it's not deprecated! Deprecated = Not current. Obsolete = no longer available.

What does jQuery submit do?

jQuery submit() Method The submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.

What is submit in JavaScript?

Definition and Usage. The submit() method submits the form (same as clicking the Submit button). Tip: Use the reset() method to reset the form.

How Prevent form submit in jQuery validation fails?

You need to do two things if validation fails, e. preventDefault() and to return false. This really works. Thanks!


1 Answers

The DOM submit() method does not trigger submit events. jQuery's does.

like image 128
Quentin Avatar answered Oct 06 '22 00:10

Quentin