[英]Disable the button after clicking the button once
在我的應用程序中,我需要在單擊按鈕后禁用該按鈕,以便用戶不應多次單擊。 APplication是MVC ASP.NET我在普通的asp.net應用程序中完成了這個。 我嘗試了下面的代碼行,但它不起作用
提前致謝
編輯我怎么能用javascript實現這個?
我發現使用IE8(我不確定其他版本的IE),如果禁用click事件上的按鈕,則無法提交表單。 所以我提出了另一種解決方案。 隱藏單擊的按鈕並在其中放置一個新的禁用按鈕。 這是代碼:
$().ready(function() {
$("someButtonSelector").click(function () {
$(this).hide();
$(this).after('<input type="button" disabled="disabled" value="' + $(this).val() + '" />');
return true;
});
});
你可以試試這個:
onclick="if (this.getAttribute('clicked') == '1') { return false; } else { this.setAttribute('clicked', '1'); }"
它不會禁用任何內容,只需在首次單擊后阻止單擊事件。
<button type="button" onclick="this.disabled = 'disabled';">Click Me!</button>
雖然Button Web控件呈現為提交:
<input type="submit" onclick="this.disabled = 'disabled';" value="Submit" />
我發現如果你禁用甚至刪除按鈕或輸入元素它將停止回調到服務器最簡單的處理方法是hide()因為你沒有刪除輸入按鈕的功能/無論如何
$('input_selector').click(function () {
$(this).hide(200/whatever);
$(this).before('<p>Waiting for server to respond?!?</p><br/>')
});
因為它是一個隱藏,所以br是為了確保p標簽位於它之上。 可能有更好的方式,我願意接受建議。
這也很好,而且很容易理解:
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;
}
}
希望能幫助到你。
這將有效:
OnClientClick="if (this.getAttribute('clicked') == '1') {
return false;
} else {
this.setAttribute('clicked', '1');
}"
@prakash你用jQuery來禁用甚至隱藏按鈕嗎?
如果您的按鈕的id是“btnClickMe”,那么以下jQuery將完成這項工作。
$("#btnClickMe").hide();
您還可以使用jQuery設置disbaled屬性;
$("#btnClickMe").attr("disabled","disabled");
上面的那個是未經測試但應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.