表单拒绝通过 jQuery 提交
我有以下表单
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原来有一个 id = 'submit' 的
按钮。 jQuery 不喜欢这样。
Turns out there was an
<input>
button with id = 'submit'. jQuery did not like this.也许是吹毛求疵,但是:
为什么你在同一行上转义
"
并且不转义"
?您是否没有将"
重写为'
并忘记转义?此外,
form
上的target
对我:)编辑:
也许
html
或其他javascript
可以给你/我们一些线索 - 否则我和你一样迷失。maybe nitpicking, but:
Why do you escape
"
and also do not escape"
on the same line? Haven't you rewritten"
to'
and forget about escaping?also,
target
onform
is something new to me :)Edit:
Maybe
html
or otherjavascript
could give you/us some clues - otherwise I feel as lost as you.尝试使用不带$()的append方法,就像这样:
另一件事,你可以将Firebug插件添加到firefox,它是调试javascript的好工具。 https://addons.mozilla.org/en-US/firefox/collection/ firebug_addons
Try to use the append method without the $(), like this:
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