使用jquery保留滚动条位置

Retaining scrollbar position using jquery

本文关键字:位置 滚动条 保留 jquery 使用      更新时间:2023-09-26

我有一个java函数,

private GroupsSelector group(LabourInputReportCriteria.Level level) {
            HtmlSelectBooleanCheckbox box = new HtmlSelectBooleanCheckbox();
            boolean isSelected = selections.isGroupSelected(level);
            box.setSelected(isSelected);
            // box.setDisabled(isDaySelectedOnFirst(level));
            String id="groupBy" + level.getClass().getSimpleName();
            box.setId(id);
            box.setOnclick("submit()");

            box.addValueChangeListener(u.addExpressionValueChangeListener("#{reportSearchCriteriaModel.groupBy}"));
            HtmlOutputText labelComponent = new HtmlOutputText();
            labelComponent.setValue(getGroupSelectionValue(level));
            tr().td();
            html(box);

            html(" ");
            html(labelComponent);
            //html("<span id='"+id+ "'></span>");
            //html("<script> function resetGroupsSelector() {  var x = document.getElementById('search_report_form:groupByWeekLevel'); alert(x); } </script>");
            endTd().endTr();
            return this;
        }

每当我点击复选框时,sumbit()就会被调用,它在后端有一些功能。现在,我的问题是,每当我点击复选框时,滚动条的位置就会向上移动,也就是说,它会在页面的顶部。我想避免这种情况。我想保持滚动条的位置不变。我该怎么做?

我试着添加下面的代码,但效果不佳。

<script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" >
var addTweet = function() {
    var scrollPosition = $(document).scrollTop();
    $('#results9016').prepend($newTweet);
    $('html, body').scrollTop(scrollPosition);
}
</script>

请帮忙。

在单击时调用的函数中,您可以说

function submit(event) {
   event.preventDefault();
   ...
}