简体   繁体   English

带有spin.js的jQuery Submit()阻止表单提交?

[英]jQuery submit() with spin.js is preventing form from submitting?

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. 我有一个提交的表单,但是当我添加jQuery代码以显示使用spin.js的加载div时,一切都会停止工作。

<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. 加载div会显示一会儿,然后像预期的那样消失,但是似乎该表单实际上不再提交了。

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. 编辑我在jQuery文档中看到了commit(),我不应该在输入字段上使用名称“ submit”。 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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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