禁用按钮后点击一次 [英] Disabling the button after once click

查看:105
本文介绍了禁用按钮后点击一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要禁用按钮,一旦它被点击,使用户无法点击它不止一次。 (我的应用程序是用ASP.NET MVC,我在一个正常的ASP.NET应用程序中做到了这一点。)

我尝试使用JavaScript和jQuery和它不工作。该按钮被停用状态,但没有被提交表单。

jQuery的:

  $(#ClickMe),ATTR(禁用,禁用)。

JavaScript的:

 函数DisableNextButton(btnId){
    的document.getElementById(btnId).disabled =真;
}

这两种方法工作的伟大,但现在的形式不会提交。


解决方案

jQuery的现在有 。一() 功能限制任何特定事件(如提交)一个发生。

例如:

  $('#myForm的')。一('提交',函数(){
    $(本).find('输入[类型=提交]')ATTR(禁用,禁用)。
});

这code会让你提交表单一次,然后禁用按钮。在查找()函数,你想停用任何按钮改变选择。

注意:每旧金山Goldenstein,我已经改变了选择的形式和事件类型提交。这使您可以从任何地方(除了按钮其他地方),同时还禁止在提交的按钮。

提交表单

注意2:每gorelog,使用 ATTR('残疾','残疾人')将prevent您的形式从发送提交按钮的值。如果你想通过按钮值,使用 ATTR('的onclick','将this.style.opacity =0.6;返回false;')而不是

I need to disable a button once it's clicked so the user can't click it more than once. (My application is written in MVC ASP.NET, I've done this in a normal ASP.NET application.)

I tried using JavaScript and jQuery and it's not working. The button is getting disabled but the form is not being submitted.

jQuery:

$("#ClickMe").attr("disabled", "disabled"); 

JavaScript:

function DisableNextButton(btnId) {
    document.getElementById(btnId).disabled = 'true';
}

Both methods work great, but now the form won't submit.

解决方案

jQuery now has the .one() function that limits any given event (such as "submit") to one occurrence.

Example:

$('#myForm').one('submit', function() {
    $(this).find('input[type="submit"]').attr('disabled','disabled');
});

This code will let you submit the form once, then disable the button. Change the selector in the find() function to whatever button you'd like to disable.

Note: Per Francisco Goldenstein, I've changed the selector to the form and the event type to submit. This allows you to submit the form from anywhere (places other than the button) while still disabling the button on submit.

Note 2: Per gorelog, using attr('disabled','disabled') will prevent your form from sending the value of the submit button. If you want to pass the button value, use attr('onclick','this.style.opacity = "0.6"; return false;') instead.

这篇关于禁用按钮后点击一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆