简体   繁体   English

单击按钮一次后禁用该按钮

[英]Disable the button after clicking the button once

In my application i need to disable the button once it is clicked so that user should not click more than once. 在我的应用程序中,我需要在单击按钮后禁用该按钮,以便用户不应多次单击。 APplication is MVC ASP.NET i have done this in normal asp.net application. APplication是MVC ASP.NET我在普通的asp.net应用程序中完成了这个。 I tried below lines of code but its not working 我尝试了下面的代码行,但它不起作用

thanks in Advance 提前致谢

edited How can i implement this using javascript ? 编辑我怎么能用javascript实现这个?

I've found that with IE8 (I'm not sure about other versions of IE) that you cannot submit a form if you disable the button on the click event. 我发现使用IE8(我不确定其他版本的IE),如果禁用click事件上的按钮,则无法提交表单。 So I came up with an alternate solution. 所以我提出了另一种解决方案。 Hide the clicked button and put a new disabled button in it's place. 隐藏单击的按钮并在其中放置一个新的禁用按钮。 Here's the code: 这是代码:

$().ready(function() {
  $("someButtonSelector").click(function () {
    $(this).hide();
    $(this).after('<input type="button" disabled="disabled" value="' + $(this).val() + '" />');
    return true;
  });
});

You can try this: 你可以试试这个:

onclick="if (this.getAttribute('clicked') == '1') { return false; } else { this.setAttribute('clicked', '1'); }"

It will not disable anything, just prevent the click event after being first clicked. 它不会禁用任何内容,只需在首次单击后阻止单击事件。

<button type="button" onclick="this.disabled = 'disabled';">Click Me!</button>

虽然Button Web控件呈现为提交:

<input type="submit" onclick="this.disabled = 'disabled';" value="Submit" />

i have found that if you disable or even remove the button or input element it will stop the call back to the server the most simplest way to handle this is a hide() because you are not removing the functionality of the input button/whatever 我发现如果你禁用甚至删除按钮或输入元素它将停止回调到服务器最简单的处理方法是hide()因为你没有删除输入按钮的功能/无论如何

    $('input_selector').click(function () {
      $(this).hide(200/whatever);
      $(this).before('<p>Waiting for server to respond?!?</p><br/>')
    });

since it is a hide the br is to make sure the p tag is on top of it. 因为它是一个隐藏,所以br是为了确保p标签位于它之上。 there may be a better way and i am open to suggestions. 可能有更好的方式,我愿意接受建议。

This works fine as well, and it's easy to understand: 这也很好,而且很容易理解:

var myButton = $('input:submit');
var waitMessage = 'wait please...';
myButton.click(function() {
    if (bottone.val() == waitMessage) {
        return false;
    };
    myButton.val(waitMessage);
    console.log('iClicked'); // this line is for testing only, it can be removed.
});

这是我调用MVC操作然后禁用自身的按钮之一:

<button  class="btn blue pull-right" onclick="location.href='@Url.Action("Index", "GetData", new { page = Model.PageNumber + 1, sortOrder = Model.CurrentSort, fromDate = Model.FromDateParam, toDate = Model.ToDateParam, terminatingpoint = Model.TerminationPointParam, SurecallNo = Model.SurecallNoParm, CallerID = Model.CallerIDParm, outcome = Model.OutcomeParm, pagesize = Model.PageSize })';this.disabled = 'disabled'" >Next ></button>
var but = 1;
function f() {
    if(but == 1) {
        alert("Hello World!");
        but = 0;
    }
}

Hope it helps. 希望能帮助到你。

This will work: 这将有效:

OnClientClick="if (this.getAttribute('clicked') == '1') {
    return false; 
} else { 
    this.setAttribute('clicked', '1'); 
}"

@prakash have you used jQuery to disable or even hide the button? @prakash你用jQuery来禁用甚至隐藏按钮吗?

If your button's id is say "btnClickMe" then the following jQuery will do the job. 如果您的按钮的id是“btnClickMe”,那么以下jQuery将完成这项工作。

$("#btnClickMe").hide();

You could also set the disbaled attribute using jQuery; 您还可以使用jQuery设置di​​sbaled属性;

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

The one above is untested but should work. 上面的那个是未经测试但应该工作。

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

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