views:

196

answers:

3

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?

A: 

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.

Adam Kiss
Oh, good point. Strange it didn't mess anything up for the other form. No difference without the slashes.
Chad Johnson
A: 

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

VinTem
A: 

Turns out there was an button with id = 'submit'. jQuery did not like this.

Chad Johnson