从按钮OnClientClick js功能启用asp.net txtbox

时间:2019-04-08 09:45:40

标签: javascript html asp.net

我想启用一个TextBox并使其在单击按钮时可见(两个的默认设置均为false)。我可以回发,但效率不高,因此我希望我的按钮激活onClientClick只是为了激活它并使它可见。

我尝试过:

JS:
function EnableTxt() {
       document.getElementById( '<%=txtSearch.ClientID%>' ).disabled = 'false';
   }
HTML:
<asp:ImageButton ID="btnSearch2" runat="server" ImageUrl="~/Images/search.png" Height="27px" Width="30px" ImageAlign="AbsBottom" OnClientClick="EnableTxt()"/>

<asp:TextBox ID="txtSearch" runat="server" CssClass="btnSearchStyle" Height="27px" Width="134px" onkeypress="return EnterEvent(event)" BackColor="#BDC3C7" BorderStyle="None" Visible="true" Enabled="false"></asp:TextBox>

我发现的JS函数没有任何作用...

2 个答案:

答案 0 :(得分:1)

将代码更改为-

<asp:ImageButton ID="btnSearch2" runat="server" ImageUrl="~/Images/search.png" Height="27px" Width="30px" ImageAlign="AbsBottom" OnClientClick="if(!EnableTxt()){return false};"/>
    <asp:TextBox ID="txtSearch" runat="server" CssClass="btnSearchStyle" Height="27px" Width="134px" onkeypress="return EnterEvent(event)"  BorderStyle="None" style="visibility:hidden" BackColor="#BDC3C7" Enabled="false"></asp:TextBox>

并将JS更改为-

 function EnableTxt()
  {
 var id = document.getElementById('<%=txtSearch.ClientID%>');
    if (id.style.visibility === "hidden" && id.getAttribute('disabled'))
    {
         id.style.visibility = "visible";
         id.removeAttribute("disabled");
    }
    else {
         id.style.visibility = "hidden";
         id.setAttribute("disabled");
         }
  }

答案 1 :(得分:0)

更改js,只需删除禁用的属性即可:

document.getElementById('<%=txtSearch.ClientID%>').removeAttribute("disabled");