简体   繁体   English

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

[英]How to disable submit button once it has been clicked?

I have a submit button at the end of the form.我在表单末尾有一个提交按钮。

I have added the following condition to the submit button:我在提交按钮中添加了以下条件:

onClick="this.disabled=true;
this.value='Sending…';
this.form.submit();"

But when it moves to the next page, the parameters did not pass and null values are passed.但是当它移动到下一页时,参数没有传递并且传递了null个值。

您应首先提交表单,然后更改提交的值:

onClick="this.form.submit(); this.disabled=true; this.value='Sending…'; "

Probably you're submitting the form twice. 可能你要两次提交表格。 Remove the this.form.submit() or add return false at the end. 删除this.form.submit()或在结尾添加return false

you should end up with onClick="this.disabled=true; this.value='Sending…';" 你应该以onClick="this.disabled=true; this.value='Sending…';"

在IE11,FF53,GC58上测试:

onclick="var e=this;setTimeout(function(){e.disabled=true;},0);return true;"

Disabled HTML forms elements aren't sent along with the post/get values when you submit the form. 提交表单时,禁用的HTML表单元素不会与发布/获取值一起发送。 So if you disable your submit button once clicked and that this submit button have the name attribute set, It will not be sent in the post/get values since the element is now disabled. 因此,如果您单击后禁用提交按钮并且此提交按钮具有设置的name属性,则不会在post / get值中发送它,因为该元素现在已禁用。 This is normal behavior. 这是正常行为。

One of the way to overcome this problem is using hidden form elements. 其中一个解决这个问题的方法是使用隐藏的表单元素。

the trick is to delayed the button to be disabled, and submit the form you can use window.setTimeout('this.disabled=true',0); 诀窍是延迟按钮被禁用,并提交你可以使用的窗体window.setTimeout('this.disabled=true',0); yes even with 0 MS is working 是的,即使有0 MS正在工作

You need to disable the button in the onsubmit event of the <form> : 您需要在<form>onsubmit事件中禁用该按钮:

<form action='/' method='POST' onsubmit='disableButton()'>
    <input name='txt' type='text' required />
    <button id='btn' type='submit'>Post</button>
</form>

<script>
    function disableButton() {
        var btn = document.getElementById('btn');
        btn.disabled = true;
        btn.innerText = 'Posting...'
    }
</script>

Note: this way if you have a form element which has the required attribute will work. 注意:这种方式如果你有一个具有required属性的表单元素将起作用。

Using JQuery, you can do this.. 使用JQuery,你可以这样做..

$("#submitbutton").click(
   function() {
      alert("Sending...");
      window.location.replace("path to url");
   }
);

I don't think you need this.form.submit() . 我认为你不需要this.form.submit() The disabling code should run, then it will pass on the click which will click the form. 应该运行禁用代码,然后它将传递将单击该表单的单击。

Another solution i´ve used is to move the button instead of disabling it. 我使用的另一个解决方案是移动按钮而不是禁用它。 In that case you don´t have those "disable" problems. 在这种情况下,你没有那些“禁用”问题。 Finally what you really want is people not to press twice, if the button is not there they can´t do it. 最后你真正想要的是人们不要按两次,如果按钮不在那里他们就不能这样做。

You may also replace it with another button. 您也可以用另一个按钮替换它。

function xxxx() {
// submit or validate here , disable after that using below
  document.getElementById('buttonId').disabled = 'disabled';
  document.getElementById('buttonId').disabled = '';
}

If you disable the button, then its name=value pair will indeed not be sent as parameter. 如果禁用该按钮,则其名称=值对确实不会作为参数发送。 But the remnant of the parameters should be sent (as long as their respective input elements and the parent form are not disabled). 但是应该发送参数的残余(只要它们各自的输入元素和父形式没有被禁用)。 Likely you're testing the button only or the other input fields or even the form are disabled? 您可能只测试按钮或其他输入字段甚至表单被禁用?


    A better trick, so you don't lose the value of the button is

    function showwait() {
    document.getElementById('WAIT').style['display']='inline';
    document.getElementById('BUTTONS').style['display']='none';
    }

wrap code to show in a div 将代码包装在div中显示

id=WAIT style="display:none"> text to display (end div) id = WAIT style =“display:none”>要显示的文本(结束div)

wrap code to hide in a div 包装代码隐藏在div中

id=BUTTONS style="display:inline"> ... buttons or whatever to hide with id = BUTTONS style =“display:inline”> ...按钮或隐藏的内容
onclick="showwait();" 的onclick = “showwait();” (end div) (结束div)

In my case this was needed. 就我而言,这是必要的。

Disable submit button on form submit 禁用表单提交上的提交按钮

It works fine in Internet Explorer and Firefox without it, but it did not work in Google Chrome. 它没有它在Internet Explorer和Firefox中正常工作,但它在谷歌浏览器中不起作用。

The problem is that you are disabling the button before it can actually trigger the submit event. 问题是您在实际触发提交事件之前禁用了该按钮。

Your question is confusing and you really should post some code, but this should work: 你的问题很混乱,你真的应该发布一些代码,但这应该有效:

onClick="this.disabled=true; this.value='Sending...'; submitForm(); return false;"

I think that when you use this.form.submit() it's doing what happens naturally when you click the submit button. 我认为当你使用this.form.submit()它正在做你点击提交按钮时自然发生的事情。 If you want same-page submit, you should look into using AJAX in the submitForm() method (above). 如果你想要同页提交,你应该考虑在submitForm()方法中使用AJAX(上面)。

Also, returning false at the end of the onClick attribute value suppresses the default event from firing (in this case submitting the form). 此外,在onClick属性值的末尾返回false禁止触发默认事件(在这种情况下提交表单)。

Here's a drop-in example that expands on Andreas Köberle's solution. 这是一个扩展了AndreasKöberle解决方案的例子。 It uses jQuery for the event handler and the document ready event, but those could be switched to plain JS: 它使用jQuery作为事件处理程序和文档就绪事件,但那些可以切换到普通JS:

(function(document, $) {

  $(function() {
    $(document).on('click', '[disable-on-click], .disable-on-click', function() {
      var disableText = this.getAttribute("data-disable-text") || 'Processing...';

      if(this.form) {
        this.form.submit();
      }

      this.disabled = true;

      if(this.tagName === 'BUTTON') {
        this.innerHTML = disableText;
      } else if(this.tagName === 'INPUT') {
        this.value = disableText;
      }
    });
  });

})(document, jQuery);

It can then be used in HTML like this: 然后它可以在HTML中使用,如下所示:

<button disable-on-click data-disable-text="Saving...">Click Me</button>
<button class="disable-on-click">Click Me</button>
<input type="submit" disable-on-click value="Click Me" />

我认为禁用按钮的简单方法是:data => { disable_with: "Saving.." }这将提交一个表单,然后禁用按钮,如果你有任何验证,如果required = 'required'. ,它也不会禁用按钮required = 'required'.

I did the trick. 我做了伎俩。 When set timeout, it works perfectly and sending all values. 设置超时时,它可以正常工作并发送所有值。

    $(document).ready(function () {
        document.getElementById('btnSendMail').onclick = function () {
            setTimeout(function () {
                document.getElementById('btnSendMail').value = 'Sending…';
                document.getElementById('btnSendMail').disabled = true;
            }, 850);
        }
    });

In this working example, the user confirms in JavaScript that he really wants to abort. 在这个工作示例中,用户在JavaScript中确认他确实想要中止。 If true, the button is disabled to prevent double click and then the code behind which updates the database will run. 如果为true,则禁用该按钮以防止双击,然后运行更新数据库的代码。

<asp:button id="btnAbort" runat="server" OnClick="btnAbort_Click" OnClientClick="if (!abort()) {return false;};" UseSubmitBehavior="false" text="Abort" ></asp:button>

I had issues because .net can change the name of the button 我有问题,因为.net可以更改按钮的名称

function abort() {
    if (confirm('<asp:Literal runat="server" Text="Do you want to abort?" />')) {
        var btn = document.getElementById('btnAbort');
        btn.disabled = true;
        btn.innerText = 'Aborting...'
        return true;
    }
    else {
        return false;
    }  
}

Because you are overriding the OnClick with OnClientClick, even if your validation method succeeds, the code behind wont work. 因为您使用OnClientClick重写OnClick,即使您的验证方法成功,后面的代码也无法正常工作。 That's why you set UseSubmitBehavior to false to make it work 这就是为什么你将UseSubmitBehavior设置为false以使其工作

PS: You don't need the OnClick if your code is in vb.net! PS:如果您的代码在vb.net中,则不需要OnClick!

$('form#id').submit(function(e){
//OnClick code 
$(this).children('input[type=submit]').attr('disabled','disabled');
e.preventDefault();
return false;
});

Okay, i did a lot of research on how to make this work perfectly.好吧,我做了很多关于如何使这项工作完美的研究。 So the best option is to create a set timeout for disabling a button onclick. Now, the problem arise when there is a submit function running on the backend.所以最好的选择是为禁用按钮 onclick 创建一个设置超时。现在,当后端运行提交 function 时,问题就出现了。 Then the events become stacked in a queue and whenever the javascript "button.disabled == true" is added to the onclick event, only the first action(ie disabling the button) gets triggered and not the submit action which is running in the backend(This backend submit function can comprise of anything such as $.ajax).然后事件堆积在队列中,每当 javascript "button.disabled == true"被添加到 onclick 事件时,只有第一个动作(即禁用按钮)被触发,而不是在后端运行的提交动作(此后端提交 function 可以包含任何内容,例如 $.ajax)。

For disabling Single button on click:要在单击时禁用单个按钮:

function() { //i always create annonymous function to avoid polluting global 
    space
    var btn = document.getElementsByClassName("btn");
    btn.onclick = function() {
        setTimeout(function() {
            backButton.disabled = true;
        }, 0);
    };
}
}();

This code will disable your button and also would run the function on the queue.此代码将禁用您的按钮,并且还会在队列中运行 function。 timeout = 0 actually is used for firing subsequent backend tasks. timeout = 0 实际上用于触发后续后端任务。

For disabling all btns in the screen:要禁用屏幕中的所有 btns:

(function() {
    let i, element, list, o;
    element = document.getElementsByClassName("classx");
    if (element) {
        element = element[0];
        list = element.getElementsByTagName("button");
        for (i = 0; i < list.length; i++) {
            o = list[i];
            o.onclick = function() {
                setTimeout(function() {
                    let i;
                    for (i = 0; i < list.length; i++) {
                        list[i].disabled = true;
                    }
                }, 0);
                return true;
            }
        }
    }
})();

This would help you disable all of the buttons present in the page.这将帮助您禁用页面中存在的所有按钮。 (Just use it according to your usecase.) Also, this(disabled button) is a good use case for settimeout=0, functionality description as it will "defer" the call until the currently "stacked javascript events" are finished. (只需根据您的用例使用它。)此外,这个(禁用按钮)是 settimeout=0 的一个很好的用例,功能描述,因为它将“推迟”调用,直到当前“堆叠的 javascript 事件”完成。

Thank you and hope this helps someone's in the future.谢谢你,希望这对将来的人有帮助。

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

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