提交使用jQuery /阿贾克斯形式只能每隔时间 [英] Submitting a form with jQuery/Ajax only works every other time

查看:114
本文介绍了提交使用jQuery /阿贾克斯形式只能每隔时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提交表单,其中包括一个文件上传通过Ajax / jQuery的,过程中,通过一个PHP脚本的形式,并在该表单原来居住在股利返回结果。

I'm trying to submit a form which includes a file upload via Ajax/jQuery, process the form through a PHP script, and return the result in the div that the form originally resided in.

我目前的形式code是:

My current form code is:

<section id="content-right">
<form name="uploader" id="uploader" method="POST" enctype="multipart/form-data">
    <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="10485760" />
    <input type="file" name="fileselect" id="fileselect" />
    <input type="submit" name="submit" id="submit" value="Upload" />
</form>
</section>

和我目前的Ajax / jQuery脚本是:

And my current Ajax/jQuery script is:

<script> 
$(function() {
$('#uploader').submit(function() { 
        $(this).ajaxSubmit({
            type: $(this).attr('method'),
            url: 'upload-song.php',
            success: function(response) {
                $('#content-right').html(response);
                }
            }); 
    return false; 
    });
});

我的PHP脚本是上传-song.php(细节并不重要)。

My PHP script is "upload-song.php" (the details don't matter).

我也有YUI.Pjax运行处理正常航行(A HREF)链接和加载这些在#内容的权利(如果用户点击任何东西,我想把它装在#内容右)。

I also have YUI.Pjax running to handle normal navigation (a href) links and load those in #content-right (if a user clicks anything, I want it loading in #content-right).

通过这种设置,通过正常的链接导航完美的作品,一切负荷#内容的权利,但上载者只能隔时间。

With this set up, navigating through normal links works perfectly, everything loads in #content-right, but the uploader only works every other time.

例如,上载将加载上载song.php在#内容正确和完美的处理一切,那么如果我导航离开该页面,并尝试上载其他项目,它不会工作,这将只是刷新页面(如果我把行动=上传-song.php的形式标记,它会加载上载song.php作为一个完整的网页,而不是在#内容右)。后刷新页面,我可以上传其他项目,这将很好地工作。

For example, the uploader will load upload-song.php in #content-right and process everything perfectly, then if I navigate away from the page and try to upload another item, it won't work, it'll just refresh the page (if I put action="upload-song.php" in the form tag it'll load upload-song.php as a full page, not in #content-right). After it refreshes the page I can upload another item and it will work perfectly.

我觉得它做的怎么样,我附上我的Ajax脚本的形式提交(因为如果我刷新页面,它完美的作品),但我没有很多的经验与这些语言,因此我不知道如何解决它。

I think it has to do with how I'm attaching my Ajax script to the form submit (because if I refresh the page it works perfectly), but I don't have a lot of experience with these languages so I'm not sure how to fix it.

另外,如果我禁用YUI.Pjax它修复了上传者,但显然打破我联系,所以我在寻找一种解决办法。

In addition, if I disable YUI.Pjax it fixes the uploader but obviously breaks my links, so I'm looking for a work around.

任何想法?

推荐答案

试试这个:

$(document).on("submit", "#uploader", function() ...

本语法会让提交事件冒泡到文档中。这样一来,当 #content_right 部分重新加载,文件保留了事件响应监听设在DOM ready函数。

This syntax will let the submit event bubble up to the document. That way, when the #content_right section reloads, the document retains the event listening response set up in the DOM ready function.

这篇关于提交使用jQuery /阿贾克斯形式只能每隔时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆