Reputation: 740
I am new in web development i am trying to handle a form submit using jquery and javascript.
My JS
$(document).ready(function () {
$('#UnitFrom').submit(function (e) {
alert(); //Works for Chrome and IE... But not in firefox...
e.preventDefault(); // Form Is not submitted in Chrome and IE.. for firefox it submits the from to same url that i am in..
});
});
My HTML
<html>
<body>
<form id="UnitFrom">
<input type="submit" id="CalculateUnit" value="Submit" />
</form>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="script.js"></script>
</body>
</html>
Any alternate to handle form submitting.
Upvotes: 1
Views: 5547
Reputation: 2647
Dude you can not have alert()
first and than e.preventDefault();
.
e.preventDefault();
has to be first and alert()
second.
jsfiddle : http://jsfiddle.net/mpatm/
Upvotes: -1
Reputation: 78630
Put something in your alert:
alert("TEXT HERE");
If you check your console, you should see an error similar to this:
Not enough arguments [nsIDOMWindow.alert]
When the exception is thrown, the method execution ends without calling preventDefault
, allowing the form to submit. This is why it is a best practice to place preventDefault
at the top of your function, but it is not required. If it is first and there is an exception, the default behavior is still prevented.
Upvotes: 2
Reputation: 87073
You should write:
$('#UnitFrom').submit(function (e) {
e.preventDefault(); // prevent default form submission
alert('some'); // do some stuff
});
OR
$('#UnitFrom').submit(function () {
alert('some');
return false; // prevent form submission
});
Upvotes: 2