0

我有一个 JS 变量 valuePercent,我从 hdnParam.Value (从 C# 获得的字符串)中获取它的值,但它不起作用。我使用 ParseInt() 将字符串转换为 int,但在输出中得到 NaN。有任何想法吗?

.ASPX.cs

 string test = "67";

 hdnParam.Value = test;

.ASPX

  <%-- Hidden field to store string to be displayed in gage--%>
   <input type="hidden" id="hdnParam" runat="server" clientidmode="Static" />
   <script>
    var valuePercent = document.getElementById("hdnParam").value; 
    var gg1 = new JustGage({
        id: "gg1",
        donut: 0,
        title: "LAKELAND",
        titleFontColor: "#000000",
        min: 0,           
        max: 100,           
        labelFontColor: "#000000",
        humanFriendlyDecimal: 1,
        decimals: 1,
        symbol: "%",
        refreshAnimationType: "bounce",
        gaugeWidthScale: 0.6,
        customSectors: [{
            color: "#ff0000",
            lo: 0,
            hi: 79
        }, {
            color: "#ffa500",
            lo: 80,
            hi: 89
        }, {
            color: "#1eae1e",
            lo: 90,
            hi: 100
        }],
        counter: true
    });
    function refreshDiv() {
        gg1.refresh(parseInt(valuePercent)); //THIS DOES NOT WORK
        gg1.refresh(45); //THIS WORKS
    }

    var myVar = setInterval(function () { refreshDiv() }, 1000);
</script>
4

2 回答 2

2

设置Page_Load上隐藏字段的值,并在OnClientClick事件触发时调用 javaScript 函数。

我的页面.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    hiddenFieldValue.Value = "this is the value";
}

标记:

<div>
    <asp:HiddenField ID="hiddenFieldValue" runat="server" />
    <asp:Button ID="ClickButton" runat="server" Text="Click Here" OnClientClick="DisplayValue()" />
</div>

脚本:

function DisplayValue() {
        var valuePercent = document.getElementById("hiddenFieldValue").value;
        alert(valuePercent);
    }
于 2013-11-25T17:56:52.183 回答
1

你能检查一下“隐藏输入”的生成ID吗?ASP.NET 可能会为 id 添加前缀,我看到您设置了 clientidmode=static 但要求确定。

如果 id 有前缀,你可能会得到它的值:

document.getElementById("<%= hdnParam.ClientID %>").value

于 2013-11-25T17:26:52.520 回答