简体   繁体   English

提交后禁用提交按钮

[英]disable submit button after submit

I'm trying to disable the submit button after the user submits a form. 我正在尝试在用户提交表单后禁用“提交”按钮。

here is my js 这是我的js

$('.dDisable').click(function(){   
 var value = $('.chars').val();
 var chars = value.length;

 if (chars<10){
         $(".dDisable").submit(function(e){
            $('.chars').css('border','1px solid red');
            return false;
        });
     }
 else{
    $('.dDisable').one('submit', function() {
        $(this).find('input[type="submit"]').attr('disabled','disabled');
        $(this).css('opacity','0.5');
       });  
    } 
 });

For some reason when the user clicks on the submit button the textarea (.chars) gets a red border and then whole form (.dDisable) gets an opacity of 0.5 由于某些原因,当用户单击“提交”按钮时,textarea(.chars)会显示红色边框,然后整个表单(.dDisable)的不透明度为0.5

Here is http://jsfiddle.net/#&togetherjs=UlcR8bJwIM 这是http://jsfiddle.net/#&togetherjs=UlcR8bJwIM

for some reason it seems like working but the form doesn't get send. 由于某种原因,它似乎可以正常工作,但表单无法发送。

If I am correct, you are trying to do some validation before submit. 如果我是正确的,则您尝试在提交之前进行一些验证。

The reason text box getting red border, following code is executed on clicking submit button 原因文本框显示红色边框,单击“提交”按钮后将执行以下代码

$(".dDisable").submit(function(e){
            $('.chars').css('border','1px solid red');
            return false;
        });

Since the above code is in "if" condition and getting passed, then your attempt to disable button is not working. 由于上述代码处于“如果”状态并通过,因此您尝试禁用按钮无效。 Because your disable it in "else" part as follows, 因为您在“其他”部分中按如下所示禁用了它,

else{
    $('.dDisable').one('submit', function() {
        $(this).find('input[type="submit"]').attr('disabled','disabled');
        $(this).css('opacity','0.5');
       });  
    } 
 }

Note: You have to refactor your code lot like, You have to write as $(this).attr('disabled','disabled'); 注意:您必须像这样重构代码,必须写为$(this).attr('disabled','disabled'); instead of $(this).find('input[type="submit"]').attr('disabled','disabled'); 而不是$(this).find('input[type="submit"]').attr('disabled','disabled');

Add this in submit event 在提交事件中添加

$(document).ready(function () {
    $("#yourFormId").submit(function () {
        $(".submitBtn").attr("disabled", true);
        return true;
    });
});

or 要么

HTML 的HTML

<form method='post'>
    <button type='submit'>Send</button>
</form>

JavaScript 的JavaScript

$('form').submit(function() {
  $(this).find("button[type='submit']").prop('disabled',true);
});

You can see the difference b/w .attr() vs .prop() 您可以看到b.w .attr()与.prop()的区别

Try this: 尝试这个:

$(document).ready(function () {
     $(".submitBtn").click(function () {
        $('input[type="submit"]').prop('disabled',true);
         $('#yourFormId').submit();
        });
     });

or 要么

$(document).ready(function () {
     $(".submitBtn").click(function () {
         $(".submitBtn").attr("disabled", true);
         $('#yourFormId').submit();
        });
     });

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

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