doufutao4428 2016-08-06 23:33
浏览 39
已采纳

使用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.

  • 写回答

1条回答 默认 最新

  • doulu4233 2016-08-07 02:05
    关注

    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);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制