Reputation: 21895
I have the following form
<form name="myForm" id="myForm" method="post" enctype="multipart/form-data" action="script.php">
and this jQuery
$(document).ready(function() {
$('#previewButton').click(function() {
// Change form's target to be in a new window.
$('#myForm').attr('target', '_blank');
/*
* Create a hidden input field and add it to the form to designate that the
* action the form is performing is a preview action.
*/
$('#myForm').append($('<input id=\"previewAction\" name="previewAction" type=\"hidden\" />'));
// Submit the form.
$('#myForm').submit();
// Change the form's target to be the current page again.
$('#myForm').attr('target', '');
/*
* Remove the hidden input field we just added so that the form can submit
* normally.
*/
$('#previewAction').remove();
return false;
});
});
I have this exact same two code on two different pages. On one, when I click my Preview link, the form submits to a new, blank window. On the other page, the form does not submit, and no window opens when I click Preview.
.click() IS running, and I know this because I put a call to alert() in .click() and was presented with an alert box.
From running the following, I can see that .submit() for my form is NOT overridden anywhere else:
var submitEvents = $('#myForm').data("events").submit;
jQuery.each(submitEvents, function(key, value) {
alert(value);
});
Also, I get no Javascript errors.
And ideas why clicking Preview (apparently) does nothing?
Upvotes: 2
Views: 1717
Reputation: 21895
Turns out there was an <input>
button with id = 'submit'. jQuery did not like this.
Upvotes: 6
Reputation: 6059
Try to use the append method without the $(), like this:
$('#myForm').append('<input id=\"previewAction\" name="previewAction" type=\"hidden\" />');
Another thing, you could you Firebug addon to firefox, it is a great tool for debugging javascript. https://addons.mozilla.org/en-US/firefox/collection/firebug_addons
Upvotes: 0
Reputation: 11859
maybe nitpicking, but:
$('#myForm').append($('<input id=\"previewAction\" name="previewAction" type=\"hidden\" />'));
Why do you escape "
and also do not escape "
on the same line? Haven't you rewritten "
to '
and forget about escaping?
also, target
on form
is something new to me :)
Edit:
Maybe html
or other javascript
could give you/us some clues - otherwise I feel as lost as you.
Upvotes: 0