【问题标题】:checkbox and textbox复选框和文本框
【发布时间】:2011-05-15 17:05:12
【问题描述】:

有一个复选框和一个文本框。最初,文本框被设置为禁用,并在选中复选框时启用文本框。我尝试过这个,但问题是添加 runat 服务器时它不起作用。或者我应该更改 asp.net 标准控件的 javascript 逻辑。或者可以建议我一种更有效的方式来做到这一点。

<script type="text/javascript">
 $(document).ready(function() {
    var checkbox = $('#htmlChkNotify');
    var textfield = $('#htmlTxtNotifyemailaddress');
        checkbox.click(function() {
            if (checkbox.is(':checked')) {
                textfield.removeAttr('disabled');
                textfield.removeClass("email");
            }
            else {
                textfield.attr('disabled', 'disabled');
                textfield.addClass("email");
            }
        });
    });


    </script>

<input type="checkbox"  id="htmlChkNotify"/>
<label for="htmlChkNotify">Notify</label>
<input type="text" id="htmlTxtNotifyemailaddress" disabled="disabled" 
       class="email" style="width:25%" />

【问题讨论】:

    标签: asp.net html textbox checkbox


    【解决方案1】:
    1. 将 runat 属性添加到您的文本框
    2. 将第 4 行更改为 var textfield = $('#');

    应该这样做

    【讨论】:

      【解决方案2】:

      有几种方法可以让它发挥作用。

      1. 使用所有服务器端代码。
      2. 在您的客户端 (javascript) 代码中,不要使用服务器端 id,而是使用控件的 .ClientId 属性。所以,如果你的htmlChkNotify 是服务器端,你可以使用var checkbox = $('#&lt;%=htmlChkNotify.ClientId%&gt;');

      【讨论】:

      • @kinn - 什么不起作用?我给了两个选项,你没有解释哪一个不起作用,什么到底不起作用。
      【解决方案3】:

      查看生成的 HTML(在浏览器中“查看源代码”)
      并检查复选框和文本框的 ID

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-10
        • 1970-01-01
        • 2012-05-28
        • 2015-05-05
        • 2014-01-10
        • 1970-01-01
        • 2022-01-02
        相关资源
        最近更新 更多