【发布时间】:2011-12-07 02:46:18
【问题描述】:
我有一个 HTML 多部分表单,其中包含名称、电子邮件和文件上传。
我想首先用 Ajax 注册用户。如果注册成功,我想继续提交表格。如果失败,我想避免提交表单。
这是我的代码:
--- HTML ---
<form id="account-form" method="post" enctype="multipart/form-data" data-ajax="false" action="upload.php" >
<input type="text" name="username" id="username" />
<input type="email" name="email" id="email" />
<input type="file" id="mediaupload" name="mediaupload" accept="image/*" />
</form>
--- JavaScript ---
accountform.submit(function(event) {
var registration_ok = true;
// See if we can register the user
// And if we can, submit the form.
$.ajax({
type: "POST",
url: "/usercreate.php",
data: postdata,
dataType: 'json',
success: function(data) {
// fine to submit the form.
},
error: function (data){
// If this fails, we don't want to submit the form!
registration_ok = false;
}
});
return registration_ok;
});
但是,它总是返回 true - 我猜这是因为 main 函数在调用 Ajax 错误之前返回。
注册失败时如何防止它返回true,并提交表单?
谢谢!
【问题讨论】:
标签: javascript jquery