通过AjaxForm提交图像并执行回调

Submit image via AjaxForm and perform callback

本文关键字:执行 回调 图像 AjaxForm 提交 通过      更新时间:2023-09-26

>我有以下函数

(inside onchange; console.log works well)
$("#prof_picture").ajaxForm(
        {
        target: '#preview',
        success: function(response){
            console.log("called");
        }
}); 
不会

调用成功函数,因此不会收到任何反馈。反馈以下列方式回显"{message:"success",action:"something",data:Array}"。有人可以帮我吗?谢谢

这是表格

<form id="profile_picture_upload" enctype="multipart/form-data" action="profile/uploadProfilePicture.php" method="post" name="prof_picture">
   <input id="profpic1" style="display:none;" name="profile_picture" type="file">
   <a id="change_profile_picture" class="profile_option" onclick="$('#profpic1').click();">Upload</a>
</form>

正如昆汀所提到的,表格没有提交。

将 ajax 绑定到正确的表单 ID 而不是表单的名称

$("#profile_picture_upload").ajaxForm(
    {
    target: '#preview',
    success: function(response){
        console.log("called");
    }
}); 

并使用此 html 提交表单

<form id="profile_picture_upload" enctype="multipart/form-data" action="profile/uploadProfilePicture.php" method="post" name="prof_picture">
   <input id="profpic1" style="display:none;" name="profile_picture" type="file" onchange="$('#profile_picture_upload').submit();">
   <a id="change_profile_picture" class="profile_option" onclick="$('#profpic1').click();">Upload</a>
</form>

尽管如果您这样做,它会点击进入下一页,因此您可能希望添加 Return false 以防止它离开此页面。