2010-05-25 110 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', '');