jquery submit(); not working for firefox

荒凉一梦 提交于 2019-12-11 08:39:31

问题


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.


回答1:


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/




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/18087721/jquery-submit-not-working-for-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!