首页 > 代码库 > javascript中遇到Cannot read property 'split' of undefined问题

javascript中遇到Cannot read property 'split' of undefined问题

function NewDate(dateTime) {            var fulldate = dateTime.split(‘-‘);            var day = fulldate[2].split(" ");            var time = day[1].split(":");            var date = new Date();            date.setFullYear(fulldate[0]);            date.setMonth(fulldate[1] - 1);            date.setDate(day[0]);            date.setHours(time[0], time[1], 0, 0);            return date;        }            var sttime, edtime;            var startTime = document.getElementById("start_time").value;            var endTime = document.getElementById("end_time").value;            sttime = NewDate(startTime);            edtime = NewDate(endTime);           

  在使用jquery-ui-timepicker控件时遇到了月份选择时控件显示没有正确切换的问题,查看了下发现报错信息为Cannot read property ‘split‘ of undefined,说明在调用NewDate函数时参数“dateTime”类型不正确,没‘split‘属性。

  然后想了下,其实就是dateTime为空。因为在启用控件时,

<input type="text" id="start_time"><input type="text" id="end_time">

两个的输入框都还为空值,导致调用的NewDate(dateTime)函数报错,所以timepicker控件异常。假如空值判断即可

var sttime, edtime;var startTime = document.getElementById("start_time").value;var endTime = document.getElementById("end_time").value;if (!startTime || !endTime) {     return;} else {     sttime = NewDate(startTime);     edtime = NewDate(endTime);}