代码之家  ›  专栏  ›  技术社区  ›  Aheho

使用客户端代码从文本框中删除只读属性

  •  0
  • Aheho  · 技术社区  · 15 年前

    我有一个默认设置为只读的多行文本框。我希望页面上有一个按钮来更改控件,以便编辑它。我希望在客户端运行此代码,因为页面呈现速度很慢,我希望避免回发。

    我遇到的问题是,我为删除readonly属性而编写的javascript代码似乎没有任何效果。我发布了一个简单的示例,说明了这个问题,供您审阅。

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Test</title>
        <script  type="text/javascript" language="javascript">
        function EnableEditing() {
        var e = document.getElementById("<%=TextBox1.ClientID%>");
            var result = e.removeAttribute("readonly");
            alert(result);
    
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>     </div>
    <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" TextMode="MultiLine">test</asp:TextBox>        
            <input id="Button1"  onclick="EnableEditing()" type="button" value="Remove RO" />
    
        </form>
    </body>
    </html>
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Pharabus    15 年前

    textbox 1是服务器端ID,

    尝试

    var e = document.getElementById("<%=TextBox1.ClientID%>");
    
     var result = e.removeAttribute("readonly",0);
    

    或者如果你不想进行不区分大小写的搜索

     var result = e.removeAttribute("readOnly");//note upercase Only
    
        2
  •  1
  •   adam0101    15 年前

    使用 var e = document.getElementById("<%=TextBox1.ClientID%>");

    此外,如果要在回发时读取修改过的文本,则不能在服务器控件上设置readonly属性。您只能在客户机上设置,如: TextBox1.Attributes("readOnly") = "readOnly";