从 JavaScript 函数获取 NaN 试图多次获取相同数据
Getting NaN from JavaScript function trying to get same data more than once
updateAction: function(postData) {
console.log("updating from custom function...");
return $.Deferred(function($dfd) {
$.ajax({
url: 'HallManagementServlet?action=UpdateStudent',
type: 'POST',
dataType: 'json',
data: postData,
success: function(data) {
$.each(data, function(entryindex, entry) {
payment_date.push(entry['payment_expires_on']);
});
var current_date = new Date();
var current_date_in_millis = current_date.getMilliseconds();
var last_payment_date = payment_date;
alert(Date.parse(last_payment_date)+"Second 1"+Date.parse(current_date));
$dfd.resolve(data);
},
error: function() {
$dfd.reject();
}
});
});
}
我试图在这里解析两个日期。但是 last_payment_date
会给我 NaN
如果我尝试两次获得该值。
当我尝试多次编辑 JTable
列表时,为什么会显示 NaN
?
last_payment_date
是 Array
类型。你需要先索引它:
Date.parse(last_payment_date[0])
看来罪魁祸首是payment_date
和Date.parse()
Date.parse()
可以处理长度为 1
的数组
Date.parse(['01-01-2015']) // 1420088400000
Why is it giving me NaN when I try to edit my JTable list more than once?
当您第一次更新 table 时 payment_date
可能只有一个条目,第二次它可能有 2 个或更多条目。
Date.parse(['01-01-2015', '03-03-2015']) // NaN
试试这个:
var data = ['10-03-2015', '01-01-2015', '03-03-2015'];
$.each(data, function(entryindex, entry) {
payment_date.push(Date.parse(entry['payment_expires_on']));
});
// ...
// Presumably the last payment date is chronologically the last one,
// but I am not sure if that is true or not.
payment_date.sort();
var last_payment_date = payment_date[payment_date.length - 1];
// last_payment_date is a now a timestamp, don't use Date.parse() on it.
// ...
updateAction: function(postData) {
console.log("updating from custom function...");
return $.Deferred(function($dfd) {
$.ajax({
url: 'HallManagementServlet?action=UpdateStudent',
type: 'POST',
dataType: 'json',
data: postData,
success: function(data) {
$.each(data, function(entryindex, entry) {
payment_date.push(entry['payment_expires_on']);
});
var current_date = new Date();
var current_date_in_millis = current_date.getMilliseconds();
var last_payment_date = payment_date;
alert(Date.parse(last_payment_date)+"Second 1"+Date.parse(current_date));
$dfd.resolve(data);
},
error: function() {
$dfd.reject();
}
});
});
}
我试图在这里解析两个日期。但是 last_payment_date
会给我 NaN
如果我尝试两次获得该值。
当我尝试多次编辑 JTable
列表时,为什么会显示 NaN
?
last_payment_date
是 Array
类型。你需要先索引它:
Date.parse(last_payment_date[0])
看来罪魁祸首是payment_date
和Date.parse()
Date.parse()
可以处理长度为 1
Date.parse(['01-01-2015']) // 1420088400000
Why is it giving me NaN when I try to edit my JTable list more than once?
当您第一次更新 table 时 payment_date
可能只有一个条目,第二次它可能有 2 个或更多条目。
Date.parse(['01-01-2015', '03-03-2015']) // NaN
试试这个:
var data = ['10-03-2015', '01-01-2015', '03-03-2015'];
$.each(data, function(entryindex, entry) {
payment_date.push(Date.parse(entry['payment_expires_on']));
});
// ...
// Presumably the last payment date is chronologically the last one,
// but I am not sure if that is true or not.
payment_date.sort();
var last_payment_date = payment_date[payment_date.length - 1];
// last_payment_date is a now a timestamp, don't use Date.parse() on it.
// ...