jquery/JavaScript - 延迟直到上传完成
我的 jQuery 脚本看起来像这样(表单正在提交要上传的文件):
$("#addMore").click(function() {
$("#progForm").submit();
$("#upTar").css("display","block");
$("#title").val("");$("#obj").val("");$("#theory").val(""); $("#code").val("");$("#output").val("");$("#conc").val("");
});
我想延迟从 $("#upTar") 开始执行代码,直到表单提交完成(即文件上传和 PHP 脚本)已回复)。
编辑
#upTar 是一个 iframe,也是表单提交的目标。我希望 #upTar 仅在从表单操作脚本生成其内容后才显示。
谢谢!
解决方案后的代码
$("#addMore").click(function() {
$("#progForm").submit();
$('#upTar').load(function() {
$("#upTar").css("display","block");
$("#title, #obj, #theory, #code, #output, #conc").val("");
});
});
My jQuery script looks something like (the form is submitting files to upload):
$("#addMore").click(function() {
$("#progForm").submit();
$("#upTar").css("display","block");
$("#title").val("");$("#obj").val("");$("#theory").val(""); $("#code").val("");$("#output").val("");$("#conc").val("");
});
I want to delay the execution of the code beginning from $("#upTar") until the form submission is completed (that is the files are uploaded and PHP script has responded).
Edit
#upTar is an iframe and the target of the form submission. I want #upTar to be displayed only after its content has been generated from the form action script.
Thanks!
Code after solution
$("#addMore").click(function() {
$("#progForm").submit();
$('#upTar').load(function() {
$("#upTar").css("display","block");
$("#title, #obj, #theory, #code, #output, #conc").val("");
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您现在正在做的是提交 HTML 表单并加载您在表单的“action”属性中列出的任何页面。 javascript 函数的下半部分永远不会实际执行,因为浏览器将被定向到新页面。这就是您想要的,一个通过 ajax 提交然后清除表单的表单:
您是否在寻找类似的东西?
What you are doing right now is submitting the HTML form and loading whatever page you have listed in the "action" attribute of the form. The lower part of the javascript function will never actually execute since the browser will be directed to the new page. Here's what you want, a form that submits via ajax and then clears the form:
Were you looking for something like that?
如果您在 iframe 内加载 jQuery,则可以使用以下函数来通过此选择器控制父元素:
本质上,您可以在 iFrame 内运行一个等待刷新的函数,此时您可以使用顶部运行任何您希望的函数.文档选择器。
If you load jQuery inside the iframe you could use the following function to control parent elements with this selector:
In essence, you could run a function inside the iFrame that waits for a refresh at which point you can run any function you wish with the top.document selector.
没有办法让浏览器中的 JavaScript “等待”任何事情。在这种情况下,您可以执行以下操作:
您可以将 CSS 更改等放入目标
的“load”事件处理程序中
您可以将代码放入表单的响应中从
本身执行的 POST。
<前><代码>$(函数() {
(函数($){
$("#upTar").css("显示","块");
$("#title, #obj, #theory, #code, #output, #conc").val("");
})(window.parent.$);
});
代码>前>
There's no way to make JavaScript in a browser "wait" for anything. In this case, there are a couple things you could do:
You could put the CSS changes etc. in a "load" event handler for the target
<iframe>
You could put code in the response to the form POST that executes from the
<iframe>
itself.