表单拒绝通过 jQuery 提交

发布于 2024-08-23 05:25:25 字数 1371 浏览 4 评论 0原文

我有以下表单

<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 错误。

以及为什么点击“预览”(显然)没有任何反应的想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撑一把青伞 2024-08-30 05:25:25

原来有一个 id = 'submit' 的 按钮。 jQuery 不喜欢这样。

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

却一份温柔 2024-08-30 05:25:25

也许是吹毛求疵,但是:

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

为什么你在同一行上转义 " 并且转义 " ?您是否没有将 " 重写为 ' 并忘记转义?

此外,form 上的 target 对我:)

编辑:

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

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.

物价感观 2024-08-30 05:25:25

尝试使用不带$()的append方法,就像这样:

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文