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

Php 使用ajax提交表单的jquery更改事件,php,jquery,ajax,forms,Php,Jquery,Ajax,Forms,这是我的表格 <form name="uploadImg" id="uploadImg" class="profile-image" enctype="multipart/form-data"> <input type="file" name="profile" id="updProfileImg"> </form> 但是更改事件没有触发表单提交,所以我尝试了trigger('submit'),但是页面正在刷新,而不是在ajax中提交。您绑定

这是我的表格

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

但是更改事件没有触发表单提交,所以我尝试了
trigger('submit')
,但是页面正在刷新,而不是在ajax中提交。

您绑定的事件不正确。正如您目前拥有的,更改字段将触发submit的绑定。它必须是这样的:

// 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();
});

一个简单的修改工作

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

                },
                success: function() {

                }
            })
        //})
    })

您应该尝试以下代码:

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

因为我希望在第一次更改中会触发一次“.change()”。

是否尝试切换返回函数?
$("#updProfileImg:file").on("change", function(){
    var queryString = new FormData($('#uploadImg')[0]);
    $.ajax({
        type: "POST",
        url: 'index.php?route=account/edit/upload',
        data: queryString,
        contentType: false,
        processData: false,
        beforeSend: function() {},
        success: function() {}
    })
});