开发者

Reading ASP.NET text box value with JavaScript

I am using a ASP.NET(3.5) page and I have a text box called txtName.

I want to read the value with JavaScript like so but it does not work.

var Name = document.getElementById(txtName).value;
alert(Na开发者_JAVA百科me);

Even this does not want to work:

var Name = document.FormName.txtName.Value;
alert(Name);

This work with plain HTML pages but not with my ASP.NET page, why?


It sounds like txtName is the server-side ID of your control. ASP.NET will use a different client-side ID when it renders the control as HTML: probably something like ctl00_Container_txtName.

You need to use the control's ClientID property to get the client-side ID, and then use that in your getElementById call:

var name = document.getElementById('<%=txtName.ClientID%>').value;


You'd better use the Jquery.then,juest call:

$('<%=txtName.ClientID%>').value;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜