Ajax表单提交和Javascript确认错误

I have a form that submits through Ajax, which works perfectly at the moment. I tried to add a confirm option to allow / prevent the Ajax submission through adding the following lines:

    var answer = confirm('Submit now?');
    return answer // answer is a boolean

    if(answer) { ... }

Below is my full function, which, as you can see, fires on clicking the submit button. The error occurs when the user selects okay in the dialog. The entire page is refreshed and any single Ajax warnings are returned at the top of a blank screen. In a normal case, without this confirm code, the error messages appear in the div#result tag at the bottom of the form.

$("#submitbtn").click(function() {

        var answer = confirm('Submit now?');
        return answer // answer is a boolean

        if(answer) { 

            $('#result').html('<img id="loading" src="images/loading.gif" />').fadeIn();
            var input_data = $('#create_po').serialize();
            $.ajax({
                type: "POST",
                url:  "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
                data: input_data,
                success: function(msg){
                    $('#loading').remove();
                    $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
                }
            });
            return false;

        }

    });

How should I implement a confirm dialog that doesn't refresh the screen? Any suggestions would be greatly appreciated. Thanks!

You are doing return answer. Which doesn't make any sense here.

It will stop the JavaScript function, and will return the boolean. Remove this line, and you're set

Also, add this to make your submit not fireing if the confirm box is false ;)

if (answer){
    // your ajax call
}
else {
    return false;
}

Do not use this:

<input type="submit">

Use this:

<input type="button">

A submit button automatically submits a form. A regular button does nothing. You can use that to listen for clicks and THEN submit your form, or not.