Reputation: 15799
I'm creating a form programmatically and trying to submit it. The submit() function does nothing, though.
$(".deleteLink").click(function(event) {
event.preventDefault();
var form = $("<form></form>");
form.attr("method", "POST");
form.attr("action", "http://myurl.com");
form.submit(function() {
alert('Handler for .submit() called.');
return false;
});
});
When I set breakpoints in Firebug, I see that the click() function gets invoked and the "form" variable gets populated correctly. Everything works right up to the submit() method. The alert() method inside the submit handler never gets called.
Code inside jquery.js does execute when submit() is called, but I had a hard time tracing through it to see what it does.
I have confirmed that there is no other object named "submit" on the page.
Anyone have a clue what's happening?
Clarification: calling submit() without the function() parameter also does nothing.
Further clarification: calling submit(function(){alert()}) followed by submit() does invoke the alert(), but the form still isn't getting submitted.
Yet another clarification: I want the entire page to refresh. I'm not trying to do an ajax submit.
Upvotes: 2
Views: 476
Reputation: 10675
You must inject the new form into the document before submitting:
$("body").append(form);
form.submit();
Upvotes: 1
Reputation: 1
Have you tried something like this?
$(".deleteLink").click(function(event) {
event.preventDefault();
$.post(
'http://myurl.com',
function(){
alert('success');
// window.location.href = 'other page';
}
);
});
Upvotes: 0
Reputation: 148120
You are attaching submit event handler instead of calling submit on form object, use this
form.submit();
You code would be
$(".deleteLink").click(function(event) {
event.preventDefault();
var form = $("<form></form>");
form.attr("method", "POST");
form.attr("action", "http://myurl.com");
form.submit(function() {
alert('Handler for .submit() called.');
});
form.submit();
});
Upvotes: 1