【问题标题】:disabled textbox禁用文本框
【发布时间】:2010-11-03 08:02:07
【问题描述】:

我写了这段代码。但它适用于 html 复选框和 txtbox。我想用 asp.net txtbox 和 checkbox 实现相同的功能。怎么做?

<script type="text/javascript">
    $(document).ready(function() {
    var checkbox = $('#notify');
        var textfield = $('#notifyemailaddress');
        checkbox.click(function() {
            if (checkbox.is(':checked')) {
                textfield.removeAttr("disabled");
                textfield.removeClass("email");
            }
            else {
                textfield.attr("disabled", "disabled");
                textfield.addClass("email");
            }
        });
    });
</script>
<div>
                               <input type="checkbox"  id="notify"/>
                              <label for="notify">Notify</label>
                              <input type="text" id="notifyemailaddress" disabled="disabled" class="email" style="width:25%" /> 
                             </div>

【问题讨论】:

标签: javascript asp.net html migrate


【解决方案1】:

您需要在 asp.net 上找到正确的 id,为此您可以使用您的 asp.net 控件的 ClientID。所以你写了类似的东西

 var checkbox = $('#<%=notify.ClientID%>');

其中 是打印控件的最终渲染 id,输入看起来像

<asp:CheckBox runat="server" id="notify" />

在 asp.net 4 上,您还可以使用静态 id 甚至避免 ClientID,但请注意不要在同一页面上多次使用相同的控件 id。你可以阅读更多:http://msdn.microsoft.com/en-us/library/1d04y8ss.aspx

【讨论】:

  • @koin 是的,这个想法是可行的,检查其他细节,如 javascript 错误。还要在浏览器上调试你的 javascript,看看你找不到什么
  • 这种事情是在单独的 .js 文件中起作用,还是仅在 ASPX 文件中起作用?
  • @Andrew ClientID,需要编译,所以不能放在静态的js脚本文件上,还需要找到'notify'控件。要在 js 文件上放置一些类似的代码,请尝试使用 stackoverflow.com/questions/2500001/…
【解决方案2】:

如果您的复选框服务器控件名称是 notifyemailaddress 然后使用:

var textfield = $('#<%= notifyemailaddress.ClientID %>');

【讨论】:

    猜你喜欢
    • 2014-12-16
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    相关资源
    最近更新 更多