e.preventdefault does not work in submit

2

I have a simple form with two inputs and a button to send it. I am trying to make the submit not work by default to be able to do things with the data before sending it to the server but, I do not know why, I can not avoid the submit.

I guess in the end it will be silly but I can not find it ...

Here the form

<form id="formID">
    <input type="email" id="loginName" placeholder="Usuario" required autofocus>
    <input type="password" id="loginPass" placeholder="Contraseña" required>

    <button type="submit">Enviar</button>
</form>

I've also tried with action="/" but it's still the same.

Here the jquery

$('#formID').on('submit', function (event) {
            event.preventDefault();
});

I've also tried it with $("#formID").submit(function(event){}); and return false but it's still the same, when you hit the button or "enter" the page is reloaded.

I do not know what else happens to me to try ... Any ideas?

    
asked by Rabegi 16.04.2018 в 17:03
source

1 answer

4

I tried with the code you provide in the post and it works correctly, maybe it's the browser cache, try cleaning by pressing Crtl + F5

Look here I leave your code, it works correctly ...

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <div class="container-fluid">
	<div class="row">
		<div class="col-lg-12">
			<form id="formID">
			    <input type="email" id="loginName" placeholder="Usuario" required autofocus>
			    <input type="password" id="loginPass" placeholder="Contraseña" required>

			    <button type="submit">Enviar</button>
			</form>
		</div>
	</div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
	<script>
		$(function() {
		  $('#formID').on('submit', function (event) {
   	          	event.preventDefault();
		  });
		});
	</script>
  </body>
</html>

PS: If you see a lot of code, it's because it's the bootstrap base template 4

    
answered by 16.04.2018 / 17:24
source