简体   繁体   中英

Enabling asp.net txtbox from button OnClientClick js function

I want to enable a textBox and make it Visible (default settings are false for both) on button click. I can postback but it's inefficient so I want my button to activate onClientClick just to activate and make it Visible.

I tried this:

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>

The JS function that I found doesn't do anything...

Change your code to-

<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>

And change JS as-

 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");
         }
  }

更改js,只需删除disable属性:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM