Grabbing Textbox in javascript
开发者_如何学编程<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
function Incrementer()
{
debugger;
var txtBox = document.getElementById('ctl00_MainContent_TextBox1').value;
alert(txtBox);
}
</script>
<asp:TextBox ID="TextBox1" runat="server" Text="0"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Up" OnClientClick="Incrementer();"/>
<asp:Button ID="Button2" Text="Down" runat="server"/>
</asp:Content>
I am unable to capture the textbox in JavaScript. What is the problem?
I've re tagged this ASP.net as well because it's quite specific to this problem.
When an ASP.net textbox control is rendered on a page it is assigned a dynamic ID that can change. There is a property for controls, ClientID
that provides you with the controls assigned ID so you can use it in your scripts.
Use as follows:
<script type="text/javascript">
function Incrementer()
{
debugger;
var txtBox = document.getElementById('<%=TextBox1.ClientID%>').value;
alert(txtBox);
}
</script>
Try this
var txtBox = document.getElementById('<%=TextBox1.ClientID%>').value;
alert(txtBox);
精彩评论