这些 jquery 语句有什么问题?

发布于 2024-08-27 21:26:41 字数 3010 浏览 4 评论 0原文

我无法让此 jquery 语句在页面加载时工作,但当我刷新 F5 页面时它会工作一次。

<div id="ResultsDiv"></div>
<div id="pager" class="pager"></div>
<input id="HfId" type="hidden" />
<script type="text/javascript">
var itemsPerPage = 5;
$(document).ready(function() {
  getRecordspage(0, itemsPerPage);
  var maxvalues = $("#HfId").val();
  alert(maxvalues);
  $(".pager").pagination(maxvalues, {
    callback: getRecordspage,
    current_page: 0,
    items_per_page: itemsPerPage,
    num_display_entries: 5,
    next_text: 'Next',
    prev_text: 'Prev',
    num_edge_entries: 1
  });
});
</script>

在初始页面加载时,alert(maxvalues);什么也没有。但是,当我刷新时,它会显示 maxvalues 的值,该值位于隐藏字段 HfId 中,因为它是在函数 getRecordspage 中分配的。

为什么这种奇怪的行为......任何建议......

编辑:

function getRecordspage(curPage) {
    $.ajax({
        type: "POST",
        url: "Default.aspx/GetRecords",
        data: "{'currentPage':" + (curPage + 1) + ",'pagesize':5}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(jsonObj) {
            $("#ResultsDiv").empty();
            $("#HfId").val("");
            var strarr = jsonObj.d.split('##');
            var jsob = jQuery.parseJSON(strarr[0]);
            var divs = '';
            $.each(jsob.Table, function(i, employee) {
                divs += '<div class="resultsdiv"><br /><span class="resultName">' + employee.Emp_Name + '</span><span class="resultfields" style="padding-left:100px;">Category&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" class="resultfields">Salary Basis&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.SalaryBasis + '</span><span class="resultfields" style="padding-left:25px;">Salary&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.FixedSalary + '</span><span style="font-size:110%;font-weight:bolder;padding-left:25px;">Address&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.Address + '</span></div>';
            });
    $(".pager").pagination(strarr[1], {
             callback: getRecordspage,
             current_page: 0,
             items_per_page: 5,
             num_display_entries: 5,
             next_text: 'Next',
             prev_text: 'Prev',
             num_edge_entries: 1
         });
            $("#ResultsDiv").append(divs);
            $(".resultsdiv:even").addClass("resultseven");
            $(".resultsdiv").hover(function() {
                $(this).addClass("resultshover");
            }, function() {
                $(this).removeClass("resultshover");
            });
            $("#HfId").val(strarr[1]);
        }
    });
}

I'm unable to get this jquery statement to work on page load but it works once when I refresh F5 the page.

<div id="ResultsDiv"></div>
<div id="pager" class="pager"></div>
<input id="HfId" type="hidden" />
<script type="text/javascript">
var itemsPerPage = 5;
$(document).ready(function() {
  getRecordspage(0, itemsPerPage);
  var maxvalues = $("#HfId").val();
  alert(maxvalues);
  $(".pager").pagination(maxvalues, {
    callback: getRecordspage,
    current_page: 0,
    items_per_page: itemsPerPage,
    num_display_entries: 5,
    next_text: 'Next',
    prev_text: 'Prev',
    num_edge_entries: 1
  });
});
</script>

On the initial pageload alert(maxvalues); is nothing. However when I refresh it shows the value of maxvalues which is in the hidden field HfId because it is assigned in the function getRecordspage.

Why this strange behaviour.... Any suggestion...

EDIT:

function getRecordspage(curPage) {
    $.ajax({
        type: "POST",
        url: "Default.aspx/GetRecords",
        data: "{'currentPage':" + (curPage + 1) + ",'pagesize':5}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(jsonObj) {
            $("#ResultsDiv").empty();
            $("#HfId").val("");
            var strarr = jsonObj.d.split('##');
            var jsob = jQuery.parseJSON(strarr[0]);
            var divs = '';
            $.each(jsob.Table, function(i, employee) {
                divs += '<div class="resultsdiv"><br /><span class="resultName">' + employee.Emp_Name + '</span><span class="resultfields" style="padding-left:100px;">Category :</span> <span class="resultfieldvalues">' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" class="resultfields">Salary Basis :</span> <span class="resultfieldvalues">' + employee.SalaryBasis + '</span><span class="resultfields" style="padding-left:25px;">Salary :</span> <span class="resultfieldvalues">' + employee.FixedSalary + '</span><span style="font-size:110%;font-weight:bolder;padding-left:25px;">Address :</span> <span class="resultfieldvalues">' + employee.Address + '</span></div>';
            });
    $(".pager").pagination(strarr[1], {
             callback: getRecordspage,
             current_page: 0,
             items_per_page: 5,
             num_display_entries: 5,
             next_text: 'Next',
             prev_text: 'Prev',
             num_edge_entries: 1
         });
            $("#ResultsDiv").append(divs);
            $(".resultsdiv:even").addClass("resultseven");
            $(".resultsdiv").hover(function() {
                $(this).addClass("resultshover");
            }, function() {
                $(this).removeClass("resultshover");
            });
            $("#HfId").val(strarr[1]);
        }
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷雾森÷林ヴ 2024-09-03 21:26:42

您的 getRecordspage 函数是异步的。它会进行 Ajax 调用,该调用完成后会设置您尝试读取的值。但是,在读取该值之前,您不会等待调用完成。

Your getRecordspage function is asynchronous. It makes an Ajax call which, when it completes, sets the value you are trying to read. However, you are not waiting for the call to complete before reading the value.

云淡月浅 2024-09-03 21:26:42

我的猜测是 $.ajax(...) success 回调在调用 alert(maxvalues); 之后执行。如果确实如此,只需将分页代码放入

$(".pager").pagination(maxvalues, {
     //my syntax
});

$.ajax(...) success 回调中即可。

My guess would be that the $.ajax(...) success callback is being executed after the alert(maxvalues); is called. If that's indeed the case, just place your pagination code

$(".pager").pagination(maxvalues, {
     //my syntax
});

in the $.ajax(...) success callback.

一刻暧昧 2024-09-03 21:26:42

如果没有有关您的问题的更多信息(缺少功能等),最好的建议是告诉您使用 firefox 并安装 firebugfirequery

然后尝试设置一些断点并检查值。这是教程

编辑:在我看到您的编辑和更多信息之前就做出了这个答案。

Without more information about your problem (missing functions, etc) the best advice is to tell you to use firefox and install firebug and firequery.

Then try to put some breakpoints and inspect values. Here's a tutorial

EDIT : made this answer before I saw your edit with more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文