将标签文本值获取到 javascript 变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17576033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Label text value into javascript variable
提问by cal5barton
I am very new to javascript but unfortunately it is what I have to use for a thermometer chart that I'm using. 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.
我对 javascript 很陌生,但不幸的是,我必须使用它来制作我正在使用的温度计图表。我需要获取 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. :) Here is what I have so far.
出于某种原因,它根本没有存储值,并且图表显然没有所需的值。我再次对 javascript 非常陌生,所以请保持友善。:) 这是我目前所拥有的。
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());
}
}
回答by catfood
var grossSales = $('#<%= MtdLBL.ClientID %>').text();
Note parentheses; text
is a function, not a simple value. The next()
is out of place unless you want the followingcontrol, which doesn't seem right.
注意括号;text
是一个函数,而不是一个简单的值。next()
除非您想要以下控件,否则这是不合适的,这似乎不正确。
回答by ponysmith
Remove the valueOf()
call in the thermometer variable definition line. valueOf
is used to return a primitive boolean. I'm assuming you're trying to use the grossSales number for something other than a boolean flag.
删除valueOf()
温度计变量定义行中的调用。 valueOf
用于返回原始布尔值。我假设您正尝试将总销售额用于布尔标志以外的其他用途。
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()