jQuery ajax 提交表单图片


基于 jQuery

 

HTML:

<form id="form" enctype="multipart/form-data">
    <input type="text" name="name" />
    <input type="text" name="desc" />
    <input type="file" name="image"  id="doc0" />

    <button type="button" id="submit" > 提交 </button>
</form>

 

JS:

// 提交表单
    $('#submit').click(function () {
        
        // 创建FormData对象
        var data = new FormData($('#form')[0]);

        // 为FormData对象添加数据
        $.each($('#doc0')[0].files, function(i, file) {
            data.append('image', file);
        });

        $.ajax({
            url:'yoururl',
            type:'POST',
            data:data,
            cache: false,
            contentType: false,        /* 不可缺 */
            processData: false,         /* 不可缺 */
            success:function(result){
                // 这里是你的成功逻辑
            }
        });
    });

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM