我有一些表单,它在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 () { …Run Code Online (Sandbox Code Playgroud)