简体   繁体   English

将标签文本值添加到javascript变量中

[英]Get Label text value into javascript variable

I am very new to javascript but unfortunately it is what I have to use for a thermometer chart that I'm using. 我是javascript的新手,但不幸的是,我必须使用我正在使用的温度计图表。 I need to get the value of an ASP label text and then store that into a javascript variable which will be used to set the chart value. 我需要获取ASP标签文本的值,然后将其存储到一个javascript变量中,该变量将用于设置图表值。

For some reason it is not storing the value at all and the chart obviously doesn't have the value needed. 由于某种原因,它根本没有存储值,图表显然没有所需的值。 Again I am extremely new to javascript so please be nice. 我再次对javascript非常新,所以请你好。 :) Here is what I have so far. :)这是我到目前为止所拥有的。

Here is part of the ASPX page: 这是ASPX页面的一部分:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script>
    window.onload = function () {
        // Create the Thermometer chart. The arguments are: the canvas ID, the minimum,
        // the maximum and the indicated value.

        var grossSales = $('#<%= MtdLBL.ClientID %>').next().text;
        var thermometer = new RGraph.Thermometer('salesThermometer', 0, 180000, parseInt(grossSales.valueOf))

        // Configure the thermometer chart to look as you want.
                    .Set('chart.gutter.left', 45)
                    .Set('chart.gutter.right', 45)
                    .Set('chart.colors', ['rgba(255,0,0,1)'])
                    .Set('chart.units.pre', '$')
        // Now call the .Draw() method to draw the chart.
                    .Draw();
    }
            </script>
<link href="Charts/RGraph/demos/demos.css" rel="stylesheet" type="text/css" />
<script src="Charts/RGraph/libraries/RGraph.common.core.js" type="text/javascript"></script>
<script src="Charts/RGraph/libraries/RGraph.thermometer.js" type="text/javascript"></script>
</asp:Content>

And this is from the code-behind: 这来自代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["TeamID"] == null || Session["TeamID"] == "")
    {
        Response.Redirect("~/Default.aspx");
    }
    else
    {
        //Populate department average
        double deptAvg = achievementData.DeptAverage();
        DeptAvgValueLBL.Text = deptAvg.ToString("P0");

        //Get sales data
        Sales getSalesData = new Sales();
        MtdLBL.Text = getSalesData.GrossSalesByTeam(Session["TeamID"].ToString());
    }
}
var grossSales = $('#<%= MtdLBL.ClientID %>').text();

Note parentheses; 注意括号; text is a function, not a simple value. text是一个函数,而不是一个简单的值。 The next() is out of place unless you want the following control, which doesn't seem right. next()不合适,除非您需要以下控件,这似乎不正确。

Remove the valueOf() call in the thermometer variable definition line. 删除温度计变量定义行中的valueOf()调用。 valueOf is used to return a primitive boolean. valueOf用于返回基本布尔值。 I'm assuming you're trying to use the grossSales number for something other than a boolean flag. 我假设您正在尝试将grossSales数用于布尔标志以外的其他内容。

See http://www.w3schools.com/jsref/jsref_valueof_boolean.asp 请参阅http://www.w3schools.com/jsref/jsref_valueof_boolean.asp

Also, as @catfood said, you don't need the .next() 另外,正如@catfood所说,你不需要.next()

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

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