JavaScript没有在else代码中读取变量值

时间:2017-03-16 14:11:08

标签: javascript if-statement

我正在尝试打印以下JavaScript的日志。但是,脚本不会打印日志值。我突出显示了脚本应在if / else代码中标识的变量值,但是在else之后它没有从$systemDate /之外读取if else声明;因此,它不会记录$dueDate的值。我正在尝试以下代码:

_wait(_div("/date-display .* bwc-selected/"));
var $date = _getText(_div("/date-display .* bwc-selected/"));

_wait(2000);

_log($date);

_wait(2000);


var str = ($date)

    var d = new Date(str)

        var $systemDate = (d.getUTCMonth()+1) +"/"+ d.getUTCDate() + "/" + d.getUTCFullYear();

_log('System Date is:' + $systemDate);




var $dueDate = person.$dueDate;

if ($dueDate != null)

{
    var $dueDate = person.$dueDate;

    person.$dueDate = $dueDate;

    _log('Due Date is:' + $dueDate);


}

else

{

    var $days = 90;

    var $theDate = new Date($systemDate);

    var $pregnancyDueDate = new Date($theDate);

    $pregnancyDueDate.setDate($pregnancyDueDate.getDate() + $days);



    var due = ($pregnancyDueDate);

    var n = new Date(due);

        var $dueDate =  (n.getUTCMonth()+1) +"/"+ n.getUTCDate() + "/" + n.getUTCFullYear();

    _log('Due Date is:' + $dueDate);


    person.$dueDate = $dueDate;


}

1 个答案:

答案 0 :(得分:1)

不要重新声明你的变量! 从

中删除var
var $dueDate =  (n.getUTCMonth()+1) +"/"+ n.getUTCDate() + "/" + n.getUTCFullYear();

它应该是:

$dueDate =  (n.getUTCMonth()+1) +"/"+ n.getUTCDate() + "/" + n.getUTCFullYear();

你的变量已经在你的if

之前声明了
var $dueDate = person.$dueDate;

最后将if条件更新为:

if ($dueDate != "")

因为您的单元格可能只是空的而且不包含空值