2

我有以下表格

<form name="myForm" id="myForm" method="post" enctype="multipart/form-data" action="script.php">

和这个 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;
  });

});

我在两个不同的页面上有完全相同的两个代码。一方面,当我单击我的预览链接时,表单会提交到一个新的空白窗口。在另一页上,表单没有提交,当我单击预览时没有打开任何窗口。

.click() 正在运行,我知道这一点是因为我在 .click() 中调用了 alert() 并出现了一个警告框。

通过运行以下命令,我可以看到我的表单的 .submit() 在其他任何地方都没有被覆盖:

var submitEvents = $('#myForm').data("events").submit;
jQuery.each(submitEvents, function(key, value) {
  alert(value);
});

此外,我没有收到 Javascript 错误。

以及为什么单击预览(显然)什么都不做的想法?

4

3 回答 3

6

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

于 2010-02-28T03:05:09.597 回答
0

尝试使用不带 $() 的 append 方法,如下所示:

$('#myForm').append('<input id=\"previewAction\" name="previewAction" type=\"hidden\" />');

另一件事,您可以将 Firebug 插件添加到 firefox,它是调试 javascript 的绝佳工具。https://addons.mozilla.org/en-US/firefox/collection/firebug_addons

于 2010-02-25T12:23:17.373 回答
0

也许吹毛求疵,但是:

$('#myForm').append($('<input id=\"previewAction\" name="previewAction" type=\"hidden\" />'));

为什么你逃跑也不"逃在同一条线上?你没有重写并忘记逃跑吗?""'

另外,targetform我来说是新事物:)

编辑:

也许html或其他javascript可以给你/我们一些线索——否则我和你一样感到迷茫。

于 2010-02-24T22:20:50.127 回答