开发者

Javascript error: unable to get value, even though it is not null

I have a bit of javascript code that gets and sets the scroll position based on a hidden input field.

<input type="hidden" id="scroll" runat="server" value="0" />

<script type="text/javascript" >
    function setScroll() {
        document.getElementById('scroll').value = document.getElementById("scrollDiv").scrol开发者_开发问答lTop;
    }

    function scrollToPos {
        document.getElementById("scrollDiv").scrollTop = document.getElementById("scroll").value;
    }

And for some reason it says the value of the hidden input field is always undefined. Does anyone have any explanations?


because you have got runat="server" in there it is probably changing the ID when its generated.

Try this instead:

<script type="text/javascript" >
    var scrollId = "<%= scroll.ClientID %>";

    function setScroll() {
        document.getElementById(scrollId).value = document.getElementById("scrollDiv").scrollTop;
    }

    function scrollToPos {
        document.getElementById("scrollDiv").scrollTop = document.getElementById(scrollId).value;
    }
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜