Getting Textbox value in Javascript

前端 未结 6 2077
执念已碎
执念已碎 2020-12-02 00:06

I am trying to get the value of the textbox in a javascript, but its not working. Given below is the code of my test page

<%@ Page Title=\"\" Language=\"V         


        
6条回答
  •  失恋的感觉
    2020-12-02 01:01

    This is because ASP.NET it changing the Id of your textbox, if you run your page, and do a view source, you will see the text box id is something like

    ctl00_ContentColumn_txt_model_code

    There are a few ways round this:

    Use the actual control name:

    var TestVar = document.getElementById('ctl00_ContentColumn_txt_model_code').value;

    use the ClientID property within ASP script tags

    document.getElementById('<%= txt_model_code.ClientID %>').value;

    Or if you are running .NET 4 you can use the new ClientIdMode property, see this link for more details.

    http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx1

提交回复
热议问题