简体   繁体   English

使用javascript设置asp.net文本框值

[英]Set asp.net textbox value using javascript

I want to set the value of a asp.net textbox using javascript 我想使用javascript设置asp.net文本框的值

My JS Code is: 我的JS代码是:

document.getElementById('<%=txtFlag.ClientID %>').value = "Track";

My textbox is: 我的文本框是:

<asp:TextBox ID="txtFlag" runat="server" Visible="False"></asp:TextBox>

But it gives me an error document.getElementById(...)' is null or not an object 但它给了我一个错误document.getElementById(...)' is null or not an object

I dont understand what is wrong. 我不明白什么是错的。

Please help. 请帮忙。

<asp:TextBox ID="txtFlag" runat="server" Visible="False"></asp:TextBox>

Setting visible=false will cause this textbox to not appear in the rendered page. 设置visible=false将导致此文本框不显示在呈现的页面中。 Remove this, and add display:none; 删除它,并添加display:none;

<asp:TextBox ID="txtFlag" runat="server" style="display:none;"></asp:TextBox>

Try including the ClientIDMode property in your textbox 尝试在文本框中包含ClientIDMode属性

<asp:TextBox ID="txtFlag" runat="server" Visible="False" 
                                     ClientIDMode="Static"></asp:TextBox>

You are calling javascript before complete document load . before complete document load调用了javascript。 Please write your javascript code on document.ready function like this 请在document.ready函数上写下你的javascript代码

 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript">

        $(document).ready(function() {
        document.getElementById('<%=txtFlag.ClientID %>').value = "Track";
        });

    </script>

And second thing is that use display none in place of visible false or use hidden field control 第二件事是使用display none代替可见的false或使用hidden field control

<asp:TextBox ID="txtFlag" runat="server" style="display:none;"></asp:TextBox>
document.getElementById('txtFlag').value='Track'

尝试这个

Solution 1: 解决方案1:

Make that textbox as visible=true and try, 将该文本框设为visible = true并尝试,

When you make a control as visible false, that control will not be loaded in client side and as you knew javascript will be executed on client side itself. 当您将控件设置为可见false时,该控件将不会在客户端加载,因为您知道javascript将在客户端本身执行。

Solution 2: 解决方案2:

Add this javascript at the end of the page. 在页面末尾添加此JavaScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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