【问题标题】:Jquery Asp.net button disableJquery Asp.net 按钮禁用
【发布时间】:2011-02-23 17:48:35
【问题描述】:

html代码:

<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 事件背后的代码不起作用?

【问题讨论】:

  • 你需要知道ClientID你在JS中的按钮。它与ID 不同,但如果您使用母版页,则类似于ctl00_Content1_btnTest
  • 问题是你必须设置disabled="disabled"属性而不是disabled="true"

标签: asp.net javascript jquery


【解决方案1】:

我认为在单击事件处理程序中禁用按钮会阻止回发。尝试一段时间后执行禁用代码:

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

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

【讨论】:

    【解决方案2】:

    尝试使用 jQuery 类选择器:

    1. CssClass="MyButton" 添加到您的 ASP.NET 按钮;
    2. 在 jQuery 中使用这个选择器
    3. 点击时设置disabled="disabled"属性

    jQuery:

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

    【讨论】:

    • 只需将 disabled 属性 true 的值替换为 disabled.attr('disabled', 'true') 替换为 .attr('disabled', 'disabled') - 这就是问题所在。
    【解决方案3】:

    示例代码使用ends-with 选择器。选择器没有错误。 你只需要像这样更改代码

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

    我已经对此进行了测试,并且工作正常,没有任何问题。

    【讨论】:

      【解决方案4】:

      我可以解决你的问题:$(".classButton").prop('disabled','disabled'); and remove disabled: $(".classButton").prop('disabled', '');

      【讨论】:

        【解决方案5】:

        您是否只需要执行以下操作:

        btnTest.Enabled = False;
        

        在代码隐藏文件中?这将导致回发,但它应该可以工作。

        【讨论】:

          【解决方案6】:

          它不起作用,因为生成的 HTML id 与 ASP.NET id 不同。
          所以 btnTest 将被呈现为另一个 Id。

          一种快速的脏方法是运行页面,查看 HTML 源代码并找到按钮生成的 Id 并将其作为参数传递给 jQuery 函数。

          更好的方法是通过后面的代码生成jQuery函数:

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

          编辑: 另外我不禁意识到您的 OnClick 属性应该指向 btnTest_Click 而不是 btn_Click

          【讨论】:

          • $= 是“ends-with”选择器,他的代码已经适用于此。
          • "btnTest_Click 而不是 btn_Click" 否
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-02-02
          • 1970-01-01
          • 1970-01-01
          • 2011-11-10
          • 1970-01-01
          • 2012-05-26
          • 2012-11-12
          相关资源
          最近更新 更多