如何限制用户提交表单两次

How to restrict user from submitting the form twice?

本文关键字:两次 表单 何限制 用户 提交      更新时间:2023-09-26

我有一个asp.net表单,它允许用户提交一个注册表单,该表单使用web服务在SharePoint列表中内部发送/存储所有这些值,因此在提交页面处理时间比正常表单稍长。

在页面重定向到感谢页面之前的平均时间,用户倾向于再次点击提交按钮,这导致多次提交同一记录。

请建议一种方法来限制这个提交,在按钮上点击我使用jquery函数进行数据验证,我已经尝试使用btn。Disable = true方法,但是一旦它到达else块(通过验证后,这是Disable = true代码所在的地方),它不会在阅读btn后提交表单。Disable = true语句

专家们,请给我指路。

提前感谢。

参见Nathan Long的插件:https://stackoverflow.com/a/4473801/1414562

{修改为允许在输入更改时重新提交表单}

// jQuery plugin to prevent double submission of forms
jQuery.fn.preventDoubleSubmission = function() {
  var $form = $(this);
  $form.on('submit',function(e){ //on here instead of bind    
    if ($form.data('submitted') === true) {
      // Previously submitted - don't submit again
      e.preventDefault();
    } else {
      // Mark it so that the next submit can be ignored
      $form.data('submitted', true);
    }
  }).find('input').on('change',function(){
       $form.data('submitted',false);
  });
  // Keep chainability
  return this;
};

像这样使用:

$('form').preventDoubleSubmission();

正如您所发现的,如果禁用onclick处理程序中的按钮,它将不会向服务器发送请求。解决这个问题的简单"技巧"是,而不是立即禁用按钮,使用setTimeout来安排一个功能运行,例如,5毫秒将禁用按钮;这将允许在按钮被禁用之前将请求发送到服务器,同时不会留下足够的时间让用户实际单击它两次。

两种方法

使用Jquery或c#

Jquery

 $("#btnSubmit").live("click",(function(e) {
    this.disabled = true;
    /Do Something /
  this.disabled = false;
return false;}));
c#

protected void btnSubmitClick(object sender, EventArgs e)
{
btnSubmit.Enabled = false;
/Do Something/
btnSubmit.Enabled = true;
}

希望有帮助

多谢安娜