jquery beforeSubmit not working

3.6k Views Asked by At

Im using html5 capacities to read image width and height before submitting... Well, this is not happening exactly, it seems like the time needed for the functions:

reader.readAsDataURL(); 
reader.onload = function();
image.onload = function();

To read the file is way too much than the time my form is able to wait before sending the pic. I can check that the beforeSubmit function is triggered, but the form is submitted before it finishes.
Another weird thing is that I have replaced the beforeSubmit function content with a simple return false sentence and the form is being submitted anyway.
Am I missing something regarding the beforeSubmit option in ajaxSubmit?

The beforeSubmit function has been minimized to a return false statement, here comes the submit (the form is inside a dialog(), may be this the clue?:

$('.block .imgpop').on("click",function()
{
    type = $(this).attr('emimage');
    currentype = type;
    options = 
    {
        type: 'POST',
        target:   '#uploadverbose',
        beforeSend:  beforeSubmit,
        resetForm: true,
        success: showResponse,
        data: { type: $(this).attr('emimage'), tempname: $(this).attr('id'), maxsize: imgsizesW[$(this).attr('emimage')] },
        dataType : "text"
    };
    $('#uploadbitch').dialog(
    {
        closeOnEscape: true,
        width: 800,
        modal: true 
    });
    return false;
});

$(document).on("click","#MyUploadForm input[type=submit]", function(e, submit)
{
        if (!submit) e.preventDefault();
        $('#MyUploadForm').ajaxSubmit(options);                                 
    });
2

There are 2 best solutions below

0
On

It may bot be exactly the answer, but it works. I have place all the image testing in the input field with a "change" event and it works just fine for me. Precocity in the form submitting is still unsolved.

2
On
$(document).on("submit","#MyUploadForm", function(e, submit)
{
         e.preventDefault();
        $('#MyUploadForm').ajaxSubmit(options);                                 
});

If you try to handle onclick on submit button then it doesn't stop form from submitting.