Getting Textbox value in Javascript

前端 未结 6 2065
执念已碎
执念已碎 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 00:41

    Use

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

    instead of

    document.getElementById('txt_model_code')`
    

    Also you can use onClientClick instead of onClick.

    0 讨论(0)
  • 2020-12-02 00:44
    <script type="text/javascript" runat="server">
     public void Page_Load(object Sender, System.EventArgs e)
        {
            double rad=0.0;
            TextBox1.Attributes.Add("Visible", "False");
            if (TextBox1.Text != "") 
            rad = Convert.ToDouble(TextBox1.Text);    
            Button1.Attributes.Add("OnClick","alert("+ rad +")");
        }
    </script>
    
    <asp:Button ID="Button1" runat="server" Text="Diameter" 
                style="z-index: 1; left: 133px; top: 181px; position: absolute" />
    <asp:TextBox ID="TextBox1" Visible="True" Text="" runat="server" 
                AutoPostBack="true" 
                style="z-index: 1; left: 134px; top: 133px; position: absolute" ></asp:TextBox>
    

    use the help of this, hope it will be usefull

    0 讨论(0)
  • 2020-12-02 00:53

    The ID you are trying is an serverside.

    That is going to render in the browser differently.

    try to get the ID by watching the html in the Browser.

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

    this may works.

    Or do that ClientID method. That also works but ultimately the browser will get the same thing what i had written.

    0 讨论(0)
  • 2020-12-02 00:57

            <script type="text/javascript">
                function MyFunction() {
                    var FNumber = Number(document.getElementById('txtFirstNumber').value);
                    var SNumber = Number(document.getElementById("txtSecondNumber").value);
                    var Sum = FNumber + SNumber;
                    alert(Sum);
                }
    
            </script>
    
            <table class="auto-style1">
                <tr>
                    <td>FirstNaumber</td>
                    <td>
                        <asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>SecondNumber</td>
                    <td>
                        <asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:TextBox ID="txtSum" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClientClick="MyFunction()" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-02 01:06

    Since you have master page and your control is in content place holder, Your control id will be generated different in client side. you need to do like...

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

    Javascript runs on client side and to get value you have to provide client id of your control

    0 讨论(0)
提交回复
热议问题