我的页面上有一些隐藏字段,除了其中一个之外,所有这些都可以正常工作。我在调用 buildGrid() 之前在 document.ready 中设置值:
$(document).ready(function() {
$.ajax(
{
type: "POST",
url: "/CDServices.asmx/GetWeekEndingDates",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(msg) {
//store default dates in hidden fields
$("#<%=hdnCurrentDate.ClientID%>").val(msg.d.CurrentDate);
$("#<%=hdnLastWeekEndingDate.ClientID%>").val(msg.d.LastWeekEndingDate);
}
});
buildGrid();
});
但是在 buildGrid 中,隐藏字段中的值为空:
function buildGrid() {
alert($("#<%=hdnLastWeekEndingDate.ClientID%>").val());
我在单击按钮时调用其他函数,其中隐藏字段值被很好地拾取,为什么该值在这里消失?
最佳答案
您应该在 success 函数中调用 buildGrid()。否则,当请求仍在运行并且您的隐藏字段尚未填充时,它将被调用。
关于javascript - 为什么我会失去隐藏字段的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5118944/