duanchun2349 2014-02-06 01:22
浏览 61

jquery更改事件以使用ajax提交表单

Here is my form

<form name="uploadImg" id="uploadImg" class="profile-image" enctype="multipart/form-data">
        <input type="file" name="profile" id="updProfileImg">
</form>

Here is my jquery event

$("#updProfileImg:file").change(function() {
    $('#uploadImg').submit(function() {
    var queryString = new FormData($('form')[0]);
    $.ajax({
        type: "POST",
        url: 'index.php?route=account/edit/upload',
        data: queryString,
        contentType: false,
        processData: false,
        beforeSend: function() {
        },
        success: function() {
        }
    })
})
})

But the change event is not triggering form submit so I tried trigger('submit') but the page is refreshing instead of submitting in ajax.

  • 写回答

3条回答 默认 最新

  • duan33360 2014-02-06 01:28
    关注

    You are binding the events incorrectly. As you currently have it, changing the field will trigger the binding of the submit. It need to be like this:

    // bind the submit event
    $('#uploadImg').submit(function() {
        var queryString = new FormData($('form')[0]);
        $.ajax({
            type: "POST",
            url: 'index.php?route=account/edit/upload',
            data: queryString,
            contentType: false,
            processData: false,
            beforeSend: function() {
            },
            success: function() {
            }
        });
    });
    
    // bind the change event to trigger a submit
    $("#updProfileImg:file").change(function() {
        $("#uploadImg").submit();
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度