简体   繁体   English

单击提交按钮后如何禁用提交按钮

[英]How to disable submit button after click submit button

I have created forms using php and using jquery for validation.What i exactly need is if validation successful on submit button, disable the submit button till submission gets over.Here is my code: 我已经使用php和jquery创建了表单来进行验证。我真正需要的是如果对提交按钮的验证成功,请禁用提交按钮直到提交结束。这是我的代码:

  if(document.location.pathname == "/contact-us"){
            $("#form-contact-user").validate({
                    ignore: [],
                    rules: {
                        name:{
                            required: true
                        },email: {
                            email: true
                        },
                        phne: {
                            phnDash: true
                        },
                        zip: {
                        zipcode: true
                        }
                    },
                    errorPlacement: function(error, element) {
                        if (element.attr("type") == "checkbox") {
                            error.appendTo( element.parent().parent().parent(".form-checkboxes"));
                        }
                        else if (element.attr("name") == "mailing_address") 
                        {
                         error.appendTo(element.parent().parent().parent());
                        }
                        else{
                            error.insertAfter(element);
                        }
                    }
            });

Try this: 尝试这个:

 $('#btnId').attr('disabled', true); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="submit" id="btnId" value="Submit"> 

According to the documentation you can use: 根据文档,您可以使用:

submitHandler (default: native form submit) : SubmitHandler(默认值:本机表单提交)

Callback for handling the actual submit when the form is valid. 表单有效时用于处理实际提交的回调。

ANSWER UPDATED 答案已更新

From your comment: 根据您的评论:

Getting the following error:-Uncaught TypeError: Cannot read property 'call' of undefined 收到以下错误:-Uncaught TypeError:无法读取未定义的属性“ call”

This message depends on your rules. 此消息取决于您的规则。 Because I don't know anything about your html I can also assume a possible structure in the following snippet: 因为我对您的html一无所知,所以我也可以在以下代码段中假设一个可能的结构:

 $("#form-contact-user").validate({ debug: true, rules: { name: { required: true, minlength: 2 }, email: { required: true }, phone: { required: true }, zip: { required: true } }, errorPlacement: function (error, element) { if (element.attr("type") == "checkbox") { error.appendTo(element.parent().parent().parent(".form-checkboxes")); } else if (element.attr("name") == "mailing_address") { error.appendTo(element.parent().parent().parent()); } else { error.insertAfter(element); } }, submitHandler: function(form) { // do other things for a valid form $(form).find(':submit').prop('disabled', true); setTimeout(function() { form.submit(); }, 1000); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script> <form id="form-contact-user" method="get" action=""> <fieldset> <legend>Please provide your name, email address, phone number and zipcode</legend> <p> <label for="name">Name (required, at least 2 characters)</label> <input id="name" name="name" type="text"> </p> <p> <label for="email">E-Mail (required)</label> <input id="email" type="email" name="email"> </p> <p> <label for="phone">Phone Number (required)</label> <input id="phone" type="text" name="phone"> </p> <p> <label for="zip">Zip code (required)</label> <textarea id="zip" name="zip"></textarea> </p> <p> <input class="submit" type="submit" value="Submit"> </p> </fieldset> </form> 

I hope this is what you are looking for, 我希望这是您想要的,

 $('#submitBtn').click(function(event) { //event.preventDefault(); $(this).attr('disabled', true); //Perform Validation //if(valid) { // $("#form-contact-user").submit(); //} }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="submit" id="submitBtn" value="Submit"> 

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

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