How to trigger submit using jQuery Form when butto

2019-08-11 00:08发布

Given this form

<form id="posts_form">
    <input type="file" value="" name="picture" >

    <span class="picture_js fc-button fc-button-prev fc-state-default fc-corner-left fc-corner-right">
        <span class="fc-button-inner">
            <span class="fc-button-content save_button">Share</span>
            <span class="fc-button-effect">
                <span></span>
            </span>
        </span>
    </span>
</form>

I'm using this <span> based button for style purposes.

What I'm trying to do is bind a click event on the <span> button and fire a submit function, like:

$('.picture_js').click(function(e){
    e.preventDefault();
    pictureUpload();
    return false;
});

function pictureUpload() {

        var options = { url: 'chat/upload' }

    $('#posts_form').ajaxForm(options);
}

But there is no response to clicking on the button (no errors on console either) -- any suggestions how to make this work?

1条回答
别忘想泡老子
2楼-- · 2019-08-11 00:51

I think you may need to include and implement both beforeSubmit and success options for file upload to work, as suggested in this post: Jquery form plugin file upload.

Try this:

function pictureUpload() {

    var options = { 
        url: 'chat/upload',
        success: function() {},
        beforeSubmit: function() {}
    }

    $('#posts_form').ajaxForm(options);
}
查看更多
登录 后发表回答