我如何禁用DIV“像提交按钮一样操作"?单击一次后? [英] How can I disable a DIV "acting like a submit button" after it is clicked once?

查看:108
本文介绍了我如何禁用DIV“像提交按钮一样操作"?单击一次后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像提交按钮一样工作的div,因此当用户单击它时,它会提交一个表单并将其向服务器发送ajax请求.我遇到的问题是,如果用户单击按钮twise,即使它是AJAX请求,它也会创建重复项.因此,我想做的就是简单地禁用,然后将其重命名为"Please wait ...",直到请求完成.以防止重复提交.

I have a div that is working like a submit button so when a user clicks it it submit a form and it sends a ajax request to the server. The problem that I am having is that if the user click the button twise, the it create duplicate even it is an AJAX request. So What I am trying to do is simply disabling the and re-naming it to, "Please wait..." until the request is completed. to prevent the double submit.

所以我的问题是在使用jQuery单击后如何重命名和禁用灰色" div?

So my question is how to rename and disable "gray out" a div after the click using jQuery?

谢谢

推荐答案

有两种方法可以使用:

  • 隐藏您的提交" div,并在其位置显示另一个请稍候..." div
  • 在div上单击时在div上设置禁用的属性,然后在提交处理程序中检查该属性

  • Hide your 'submit' div and show a different, 'Please wait...' div in its place
  • Set the disabled property on the div when it's clicked, and check for that property in the submit handler

$("div.submit").click(function(e) {
    if ($(this).prop("disabled"))
        return false;
    // any other error checking, setup etc...
    $(this).prop("disabled", true);
    // submit the request...
});

您还可以使用全局标志或jQuery的.data()函数来标记何时提交表单,但是我建议使用属性方法.请求完成后,您可以将Disabled属性设置为false,从而可以根据需要在以后重新提交表单.

You could also use a global flag or jQuery's .data() function to mark when the form has been submitted, but I'd recommend the property approach. Once the request has completed, you can then set the disabled property to false, thus allowing the form to be resubmitted later, if desired.

这篇关于我如何禁用DIV“像提交按钮一样操作"?单击一次后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆