Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  3] [ 4]  / answers: 1 / hits: 109741  / 13 Years ago, thu, june 9, 2011, 12:00:00

So can someone please tell why neither of these options will actually submit the form? I am trying to do something more complicated but I have boiled it down to this to try and figure out why I can't seem to get this form to submit using a click event and submit()



<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv=Content-Type content=text/html; charset=utf-8 />
<title>Untitled Document</title>
<script type=text/javascript src=https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js></script>
<script type=text/javascript>
$(function() {
$('#submitButton').click(function(e) {
e.preventDefault();
$(#testForm).submit();
});

$('#submitLink').click(function(e) {
e.preventDefault();
$(#testForm).submit();
});
});
</script>
</head>

<body>
<form action=javascript:alert('submitted'); method=post id=testForm>
<label>Name</label>
<input type=text name=name value= />
<input type=submit name=submit value=Submit id=submitButton />
<p><a href=# id=submitLink>Submit Form</a></p>
</form>

</body>
</html>


Thank you!


More From » jquery

 Answers
12

If you have a form action and an input type=submit inside form tags, it's going to submit the old fashioned way and basically refresh the page. When doing AJAX type transactions this isn't the desired effect you are after.



Remove the action. Or remove the form altogether, though in cases it does come in handy to serialize to cut your workload. If the form tags remain, move the button outside the form tags, or alternatively make it a link with an onclick or click handler as opposed to an input button. Jquery UI Buttons works great in this case because you can mimic an input button with an a tag element.


[#91781] Wednesday, June 8, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gideons

Total Points: 197
Total Questions: 106
Total Answers: 108

Location: Moldova
Member since Sat, Jan 29, 2022
2 Years ago
;