单击验证后禁用提交按钮?

时间:2012-12-19 19:00:00

标签: javascript jquery

我有一个表单,它将数据传递给cc处理终端,以及编写PDF合同和收据。表单中内置了验证,我试图添加一个函数,在一次点击后禁用提交按钮,并提供验证。

我对帖子数据了解不多,但我想确定一下:

1. Still posts the data. 2. Still validates. 3. Does not allow the user to submit multiple times.

这是我的POST代码块:

<form method="POST" action="partner_register_ihg.php" onSubmit="javascript:return WebForm_OnSubmit();"  id="docContainer" autoeventwireup="true" enctype="multipart/form-data" novalidate class="fb-toplabel fb-100-item-column fb-large selected-object" style="width: 800px;" data-form="preview">

这是我的提交部分

<input type="submit" name="subbutton" onClick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("subbutton", "", true, "", "", false, false))' id="subbutton" class="fb-button-special" value="Submit" />

以下是我验证码的一小部分

var RequiredFieldValidator30 = document.all ? document.all["RequiredFieldValidator30"] : document.getElementById("RequiredFieldValidator30");
RequiredFieldValidator30.controltovalidate = "item56_text_1";
RequiredFieldValidator30.errormessage = "Please enter the shipping zip code.";
RequiredFieldValidator30.display = "None";
RequiredFieldValidator30.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
RequiredFieldValidator30.initialvalue = "";
var RequiredFieldValidator31 = document.all ? document.all["RequiredFieldValidator31"] : document.getElementById("RequiredFieldValidator31");
RequiredFieldValidator31.controltovalidate = "item62_0_checkbox";
RequiredFieldValidator31.errormessage = "Please agree to Terms and Conditions.";
RequiredFieldValidator31.display = "None";
RequiredFieldValidator31.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
RequiredFieldValidator31.initialvalue = "";

var ValidationSummary1 = document.all ? document.all["ValidationSummary1"] : document.getElementById("ValidationSummary1");
ValidationSummary1.showmessagebox = "True";
ValidationSummary1.showsummary = "False";

3 个答案:

答案 0 :(得分:6)

在提交按钮代码中添加javascript代码: <input type="submit" [...] id="btn1" onclick="setTimeout(disableFunction, 1);"> 并在Javascript中添加功能:

function disableFunction() {
    document.getElementById("btn1").disabled = 'true';
}

答案 1 :(得分:1)

如果您使用的是jQuery表单验证程序,则可以

$(document).ready(function () {
    $("#addOfficerForm").submit(function () {
        var total_error = document.querySelectorAll('.error').length;
        if(total_error === 0){
            document.getElementById("checkEditRestriction").disabled = 'true';
        }  
    });
});

答案 2 :(得分:0)

HTML:

<button type="button" id="btnSubmit"> Submit </button>

Jquery/js:

$('#btnSubmit').click(function(e){
 e.preventDefault();
 if(this.form.reportValidity()){
  $(this).prop('disabled',true);
  this.form.submit();
 }
});