简体   繁体   English

无法获取文本框的值并将我放入JavaScript中的变量

[英]cant get the value of textbox and put i into a variable in JavaScript

I set the value of the textbox in Page-load and assign it into a variable in the java script. 我在“页面加载”中设置文本框的值,并将其分配给Java脚本中的变量。 but this line wont execute. 但是这一行不会执行。

        y = document.getElementByID("<%= TextBox1.ClientID %>").value;

it wont work either when I set value of textbox in the aspx page. 当我在aspx页面中设置textbox的值时,它都不起作用。

    protected void Page_Load(object sender, EventArgs e) {
 TextBox1.Text = "14";
 } 

and

 <asp:TextBox ID="TextBox1" runat="server" Text="14"></asp:TextBox>

neither of them worked -------- here is the script ----- 他们俩都不工作--------这是脚本-----

     <script type="text/javascript">
                                var map;
                                var y;
                    y = document.getElementByID("<%= TextBox1.ClientID %>").value;
                    y = parseInt(y);

                require(["esri/map", "dojo/domReady!"], function (Map) {

                    esriConfig.defaults.map.panDuration = 1;   

                        var map = new Map("map", {
                        center: [-118, y],
                        zoom: 15,
                        basemap: "satellite"

                    });
                });
            </script>

what am i doing wrong? 我究竟做错了什么? Everything works OK when I just assign y to a number , Is this even a good practice to get values from asp controls? 当我仅将y分配给数字时,一切正常,这甚至是从ASP控件获取值的一种好习惯吗?

"<%= TextBox1.ClientID %>"

This is not getting evaluated like you think, it will just send this as a literal string to the client. 不会像您想的那样对它进行评估,它只会将它作为文字字符串发送给客户端。 Instead you should do: 相反,您应该这样做:

y = document.getElementByID("'" + <%= TextBox1.ClientID %> "'").value;

Even so, this is quite ugly code. 即使这样,这也是非常丑陋的代码。 Since the textbox does not seem to be a dynamically generated contol, you should set it's ClientIDMode property to Static or Predictable . 由于文本框似乎不是动态生成的控件,因此应将其ClientIDMode属性设置为StaticPredictable Then hard-code the ID in your javascript. 然后在您的JavaScript中将ID硬编码。

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

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