我正在使用javascript获取hiddenfield的值

<script type="text/javascript">
        $(function() {
            var newYear = document.getElementById('HF');
            alert('hehe' + newYear);
            $('#countdown').countdown({ until: newYear, format: 'DHMS', layout:
'<div id="timer">' + '<hr />' +
    '<div id="timer_days" class="timer_numbers">{dnn}</div>' +
    '<div id="timer_hours" class="timer_numbers">{hnn}</div>' +
    '<div id="timer_mins" class="timer_numbers">{mnn}</div>' +
    '<div id="timer_seconds" class="timer_numbers">{snn}</div>' +
'<div id="timer_labels">' +
    '<div id="timer_days_label" class="timer_labels">days</div>' +
    '<div id="timer_hours_label" class="timer_labels">hours</div>' +
    '<div id="timer_mins_label" class="timer_labels">mins</div>' +
    '<div id="timer_seconds_label" class="timer_labels">secs</div>' +
'</div>' +
'</div>'
            });
        });
</script>


这是aspx代码

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div class="maindeal">
            <input type="hidden" runat="server" id="HF" />
                <div class="SocialNetworkShare">
                    <asp:Label ID="SocialNetworkShare" runat="server" Text="Share" CssClass="SNS"></asp:Label>
                </div>


后面的代码[我正在后面的代码中设置隐藏字段的值]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            ShowOfferDetails(System.Configuration.ConfigurationManager.AppSettings.Get("DefaultOffer"), True)          '... here "0;0;0" is given as there is no event argument
        End If
        HF.Value = "10/21/2011"
        MsgBox(HF.Value)
    End Sub


问题是javascript无法检测到该值...并且当我使用alert()检查该值时会引发null异常..请帮助。

最佳答案

您的HF控件是服务器端的,因此它在DOM中的ID不仅是HF。
您可以做的是在HF控件中放置一个class="HF"属性,然后从JS中执行
$(".HF").val()获得其价值

09-16 09:50