暂停表单提交以进行验证

mas*_*don 6 django validation ajax jquery form-submit

我有一些表单,它在iframe中上传文件.我想保留它的提交,直到我检查它是否与ajax有效.我怎样才能做到这一点 ?我的代码暂停提交并返回验证结果(当前只是一个返回'result':'true'的虚函数)但是然后不执行'submit'操作.在获得响应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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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


编辑

使用下面的代码,我能够执行验证,然后当它的结果是肯定的形式提交.现在,如果我对表单进行硬编码,我的警报就不会显示,而且我很确定上传没有被执行(不幸的是,上传列表每8小时刷新一次,如果它在一段时间内有效,我会知道).如果未指定target,则使用重定向上载文件,以便省略整个"submit"事件侦听器.

<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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Jam*_*son 0

我认为您可能对表格的提交考虑过多,但我也可能读错了。我也可能没有掌握所有事实。通过ajax调用提交表单并返回提交结果(验证失败或成功)不是更好吗?

伪代码:

jQuery:

var formData = data;
ajax(formData);
Run Code Online (Sandbox Code Playgroud)

表格处理:

if(valid) {
  submit(formData);
  return true;
} else {
  return false;
}
Run Code Online (Sandbox Code Playgroud)

如果需要,我可以提供真实的代码。