2010-05-25 112 views
6

的html代碼:jQuery的Asp.net按鈕禁用

<asp:Button runat="server" ID="btnTest" Text="Test" OnClick="btnTest_Click" /> 

jQuery代碼:

$('[id$=btnTest]').click(function(){ 
    $('[id$=btnTest]').attr('disabled', 'true'); 
}); 

代碼隱藏:

protected void btnTest_Click(object sender, EventArgs e) 
{ 
//here not come. 
} 

代碼爲btnTest事件背後不行?

+0

你需要知道你的'在JS按鈕ClientID'。它與ID不同,但是像'ctl00_Content1_btnTest',如果你使用masterpage的話。 – 2010-05-25 14:57:06

+0

問題是你必須設置'disabled =「disabled」'屬性而不是'disabled =「true」'。 – 2010-05-25 15:01:04

回答

8

我認爲在click事件處理程序中禁用按鈕會阻止回發。嘗試一段時間後執行禁用代碼:

$('[id$=btnTest]').click(function(){ 
    var button = this; 

    setTimeout(function() { 
     $(button).attr('disabled', 'disabled'); 
    }, 100); 
}); 
-2

這不起作用,因爲生成的HTML標識與ASP.NET標識不同。
所以btnTest將被渲染爲另一個Id。

一個快速骯髒的方法是運行該頁面,查看HTML源代碼並找到該按鈕生成的Id並將其作爲jQuery函數中的一個參數傳遞。

一個更好的方法是通過生成的代碼背後的jQuery函數:

Literal1.Text = "$('[id$=" + btnTest.ClientId + "]').click(function(
{$(this).attr('disabled', 'disabled');});"; 

編輯: 我也忍不住意識到你的OnClick屬性應指向btnTest_Click而不是btn_Click

+1

'$ ='是「ends-with」選擇器,他的代碼已經適用於此。 – 2010-05-25 14:55:21

+0

「btnTest_Click and not btn_Click」no – Chicharito 2010-05-25 14:58:51

0

難道你不會ed執行以下操作:

btnTest.Enabled = False; 

in the code-behind file?這將導致回發,但它應該工作。

+0

我不想回傳 – Chicharito 2010-05-25 14:55:06

1

嘗試使用jQuery類選擇:

  1. 添加CssClass="MyButton"到您的ASP.NET按鈕;
  2. jQuery中的點擊
  3. disabled="disabled"屬性

jQuery的使用這個選擇:

$('button.MyButton').click(function(){ 
    $(this).attr('disabled', 'disabled'); 
}); 
+0

只需用** .attr替換** disabled **:'.attr('disabled','true')''disabled'屬性* true *的值('禁用','禁用') - 這就是問題所在。 – 2010-05-25 15:09:43

1

的示例代碼使用斷頭與選擇。選擇器沒有錯誤。 你只需要改變這樣的

$('[id$=btnTest]').click(function() { 
     $('[id$=btnTest]').attr('disabled', true); 
}); 

我測試了這一點,並沒有任何問題,工作正常的代碼。

0

我可以解決你的問題:$(".classButton").prop('disabled','disabled'); and remove disabled: $(".classButton").prop('disabled', '');