使用spin.js的jQuery submit()阻止表单提交?

使用spin.js的jQuery submit()阻止表单提交?

问题描述:

I have a form that submits just fine, but when I add jQuery code to show a loading div using spin.js everything stops working.

<form id="search_form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<!-- Form inputs here -->
...
<input id="exam_search" type="submit" name="submit" value="Search" />   

Once I add the following code the form stops submitting and nothing happens. The loading div shows for a brief moment and then goes away like expected, but it seems like the form isn't actually submitting anymore.

var opts = // Array of options 
var spinner = null;
$("#search_form").submit(function() {
    spinner_div = document.getElementById('spinner');
    if(spinner == null) {
        spinner = new Spinner(opts).spin(spinner_div);
        $("#search_results, #query").css({"opacity": "0.75"});
        $("#search_form :input").attr("disabled", true);

    } else {
        spinner.spin(spinner_div);
        $("#search_results, #query").css({"opacity": "0.75"});
        $("#search_form :input").attr("disabled", true);
    }
});

If I change all of that code in the submit event to this:

$("#search_form").submit(function() {
    alert ("form submitted");
});

It shows the alert and then returns the results of the form submission just fine.

What am I doing wrong here?

EDIT I saw in the jQuery docs for submit() that I shouldn't use the name "submit" on the input field. I tried changing that as well with no luck.

Well I'm not sure why but simply doing this worked:

$("#search_form").submit(function() {
    var spinner_div = document.getElementById('spinner');
    var spinner = new Spinner(opts);
    spinner.spin(spinner_div);
});