这些 jquery 语句有什么问题?
我无法让此 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 :</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]);
}
});
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
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.我的猜测是 $.ajax(...)
success
回调在调用alert(maxvalues);
之后执行。如果确实如此,只需将分页代码放入$.ajax(...)
success
回调中即可。My guess would be that the $.ajax(...)
success
callback is being executed after thealert(maxvalues);
is called. If that's indeed the case, just place your pagination codein the $.ajax(...)
success
callback.如果没有有关您的问题的更多信息(缺少功能等),最好的建议是告诉您使用 firefox 并安装 firebug 和 firequery。
然后尝试设置一些断点并检查值。这是教程
编辑:在我看到您的编辑和更多信息之前就做出了这个答案。
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.