简体   繁体   中英

Call function on submit of ajax form

I'm using the ajaxForm.js to make an ajax image upload.

this is the js function I use:

$(document).ready(function() {
    $('#photoimg').live('change', function() {
        $("#hd_pic").html('');
        $("#loading").html('<img src="../img/common/loader.gif"/>').fadeIn(250);
        $("#imageform").ajaxForm({
            target: '#hd_pic'  
        }).submit();
    });
});

I don't want to output the result to target: '#hd_pic', but I want to call a function. For example, I do it this way in my ajax call:

$.ajax({
    type: "POST",
    url: "/ajax/add_url.php",
    data: dataString,
}).done(function(result) {
    myresult(result);
});​

I call the function myresult and I use the result of the ajax call. I also want to do this in the ajaxform submit function. Is it possible to do this?

This doesn't work:

        $("#imageform").ajaxForm({
        success: myresult(result)
    }).submit();

Instead of target , you can use a success callback property as one of the options :

$("#imageForm").ajaxForm({
    success: function() {
        // your callback code goes here
    }
});

I think you can do:

$("#imageform").ajaxForm({
    success: myresult
}).submit();

exact syntax you want

 $("#imageform").ajaxForm({
        target: '#hd_pic',
        success: function(){/*your code after here*/}
    })

jQuery API: Ajax Options

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM