我有一个文件上传表单,我想使用 JQuery 表单通过 Ajax/iframe 提交。当它很简单时,我让它工作,但现在我添加了更多,它不起作用。
我的表单设置为,当您单击字段标签时,会弹出文件上传对话框。当您选择一个文件时,将提交表单。我收到警报说“已更改!” 和“准备!”,然后什么都没有。
我已经尝试使用 Safari 的开发人员工具对其进行调试,但我看不出有什么问题。我尝试使用 ajaxForm 而不是 ajaxSubmit,但没有任何区别。没有任何报告的问题,我没有看到任何错误。
这是我的表单(它是 Django 模板的一部分,但我已将实际标签和输入放入其中,以便您查看它的外观):
<form action="{% url "edit_profile_section" user_id=end_user.pk section=section %}" method="POST" enctype="multipart/form-data" id="picture_form" encoding="multipart/form-data">
{% csrf_token %}
<label class="edit-profile-picture" for="id_profile_picture"><span>Edit Profile Picture</span></label>
<input id="id_profile_picture" name="profile_picture" />
<button type="submit" name="picture_form" id="picture_form_submit">Save</button>
</form>
这是JavaScript:
<script src="/static/js/jquery-1.10.2.min.js"></script>
<script src="/static/js/jquery.form.js"></script>
<script>
$(document).ready(function() {
var options = {
iframe: true,
dataType: 'json',
beforeSubmit: showPopup,
success: editImage
}
$('#id_profile_picture').change(function() {
alert('changed!');
$('#picture_form').ajaxSubmit(options);
});
function showPopup(formData, jqForm, options) {
alert('preparing!');
return true;
}
function editImage(responseText, statusText, xhr, $form) {
alert('status:' + statusText + ', response:' + responseText);
}
});
</script>