jquery - 暂停表单提交以进行验证

标签 jquery ajax django validation form-submit

我有一些表单,可以在 iframe 中上传文件。我想保留它的提交,直到我检查它是否对 ajax 有效。我怎样才能做到这一点 ?我的代码暂停提交并返回验证结果(当前只是一个返回“结果”:“true”的虚拟函数),但随后不执行“提交”操作。另外,在获取响应 200 状态后需要大约 2 秒才能显示响应数据,这是否正常?

这是我的 html:

<div id="message" style="background:black; width:400px; height:80px; display:none; color:white;">
</div>
<h1>Submit</h1>
<form action="{{upload_url}}" target="upload_target" method="POST" id="file_upload_form" enctype="multipart/form-data">
    {% render_upload_data upload_data %}
    <table>{{ form }}</table>    
    <p>
        <input type="hidden" maxlength="64" name="myfileid" value="{{ myfileid }}" >
    </p>
    <p>
        <input type="submit" id="file_upload_submit" value="Submit" />
    </p>
    <iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>

Js:

$(function() {

    $('#file_upload_submit').click(function(e){
        e.preventDefault();
        var fileUploadForm = document.getElementById('file_upload_form');

        $.ajax({
            type: "POST",
            url: '/artifact/upload/check-form/',
            data: 'foo=bar',
            dataType: "json",
            success: function(data){
                if(data['result'] == "true"){
                    $("#message").show();
                    $("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
                    setTimeout(function(){
                        $("#message").fadeOut("slow", function () {
                            $("#message").hide();
                        });
                    }, 1500);
                     $('#file_upload_form').attr('target','upload_target').submit(function(){
                        alert('Handler for .submit() called.');
                        return false;
                    });
                }
                else{
                    $("#message").show();
                    $("#message").fadeIn(400).html('<span">'+data["message"]+'</span>');
                    setTimeout(function(){
                        $("#message").fadeOut("slow", function () {
                            $("#message").hide();
                        });
                    }, 1500);                    
                    return false;
                }
            }
        });
        return false;        
    });

});    

</script>

和链接: http://ntt.vipserv.org/artifact/

<小时/>

编辑

使用下面的代码,我可以执行验证,然后当结果为肯定时提交表单。现在,如果我对表单的目标进行硬编码,则不会显示我的警报,并且我非常确定上传不会执行(不幸的是,上传列表每 8 小时刷新一次,我会知道它是否在某个时间起作用)。如果未指定目标,则使用重定向上传文件,因此忽略整个“提交”事件监听器。

<script>  
    $('#fake_upload_submit').click(function(e){
        e.preventDefault();

        var fileUploadForm = document.getElementById('file_upload_form');
        fileUploadForm.addEventListener("submit", function() {
            alert("sent in iframe");
            fileUploadForm.target = 'upload_target';
        }, false);       

        $.ajax({
            type: "POST",
            url: '/artifact/upload/check-form/',
            data: 'foo=bar',
            dataType: "json",
            success: function(data){
                if(data['result'] == "true"){
                    $("#message").show();
                    $("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
                    setTimeout(function(){
                        $("#message").fadeOut("slow", function () {
                            $("#message").hide();
                        });
                    }, 1500);

                    fileUploadForm.submit();

                }
                else{
                    $("#message").show();
                    $("#message").fadeIn(400).html('<span">Response false</span>');
                    setTimeout(function(){
                        $("#message").fadeOut("slow", function () {
                            $("#message").hide();
                        });
                    }, 1500);                    
                    return false;
                }
            }
        });
        return false;        
    });   
</script>

html:

<div id="message" style="background:black; width:400px; height:80px; display:none; color:white;">
</div>
<h1>Submit</h1>
<form action="{{upload_url}}" method="POST" target="" id="file_upload_form" enctype="multipart/form-data">
    {% render_upload_data upload_data %}
    <table>{{ form }}</table>    
    <p>
        <input type="hidden" maxlength="64" name="myfileid" value="{{ myfileid }}" >
    </p>
    <p>
        <input type="submit" style="display:none" id="true_upload_submit" value="Submit" />
    </p>    
    <iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
    <p>
        <input type="submit" id="fake_upload_submit" value="Submit" />
    </p>

最佳答案

验证后,您注册了一个新的提交事件处理程序,但没有任何内容提交表单,因此您必须再次单击该按钮才能提交它。

您可以只提交表单,而不添加提交处理程序:

$('#file_upload_form').attr('target','upload_target').submit();

关于jquery - 暂停表单提交以进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4205574/

相关文章:

jquery - CSS、jQuery 和 IE。

c# - 在将 JS 用于 REST API 的 ASP MVC 应用程序中处理安全性

javascript - str_replace 来自 Ajax 调用数据的 js 内部

Django 模板 - 动态变量名

javascript - 单击按钮时我的表单总是提交,无论我做什么

javascript - Javascript 中的 jQuery POST 显示错误

jquery - jQuery 函数的 $.ajaxSetup 冲突

python - 将 django 升级到 1.6.5 后出现 django object is not JSON serializable 错误

python - Django: ConnectionAbortedError: [WinError 10053] 已建立的连接被主机中的软件中止

javascript - 拖放 jquery UI