温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - Function does not enter the else

javascript - 函数不输入其他

发布于 2020-03-27 10:30:25

我在datatables表中使用以下功能

function novaData(d){
   var dia  = d.getDate().toString();
   dia = (dia.length == 1) ? '0'+dia : dia;
   var mes  = (d.getMonth()+1).toString();
   mes = (mes.length == 1) ? '0'+mes : mes;
   var ano = d.getFullYear();
   return dia+"-"+mes+"-"+ano;
}

  class CellDate{
    constructor( start_date ){
        this.date = start_date;
    }
getNextDate(){
   if($('#user_data tbody tr').length){
      var ultima_data = $("#user_data tbody tr:first td:first").text().trim().split("-");
      var ultimo_dia = +ultima_data[0];
      var ultimo_mes = +ultima_data[1];
      var ultimo_ano = +ultima_data[2];
      this.date.setDate(ultimo_dia);
      this.date.setMonth(ultimo_mes-1);
      this.date.setYear(ultimo_ano);
      this.date.setDate( this.date.getDate()+1);
   }else{
      this.date.setMonth(this.date.getMonth()+1);
      this.date.setDate(1);
   }
   return  novaData(this.date);
}
}

  var DateIndexer = new CellDate(new Date(Date.now()));

$('#add').click(function(){

   var html = '<tr>';
   html += '<td contenteditable id="data1">'+DateIndexer.getNextDate()+'</td>';
   html += '<td contenteditable id="data2"></td>';
   html += '<td contenteditable id="data3"></td>';
   html += '<td contenteditable id="data4"></td>';
   html += '<td contenteditable id="data5"></td>';
   html += '<td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs"><span class="glyphicon glyphicon-send"></span></button></td>';
   html += '</tr>';
   $('#user_data tbody').prepend(html);
  });

在中的类中getNextDate,如果该属性为true(表已经有行),则创建新行将使用已输入的最后一行的第二天的日期填充date字段。

但是,如果该表未插入任何行,则它将在date字段中返回NaN-NaN-NaN,因为它没有输入else

如果放在console.log(DateIndexer.getNextDate());函数$('#add').click(function(){返回之前01-08-2019

但是如果放到console.log(DateIndexer.getNextDate());函数里面就$('#add').click(function(){返回NaN-NaN-NaN

#user_data

<div class="container box">
   <h3 align="center">MAPA DE TRABALHO</h3>
   <h5 align="center">Ajudante de Ação Direta - Turno: Manhã</h5>
   <br />
   <div class="table-responsive">
   <br />
    <div align="right">
     <button type="button" name="add" id="add" class="btn btn-info"><span class="glyphicon glyphicon-plus"></span></button>
    </div>
    <br />
    <div id="alert_message"></div>
    <table id="user_data" class="table table-bordered table-striped">
     <thead>
      <tr>
       <th>Data</th>
       <th>Resp. de Turno</th>
       <th>Apoio</th>
       <th>Elementos ALA A</th>
       <th>Elementos ALA B</th>
       <th></th>
      </tr>
     </thead>
    </table>
</div>
</div>
</div>

查看更多

查看更多

提问者
Bruno Pinto
被浏览
20
Cleiton Tortato 2019-07-04 00:21

您的html没有tbody,第一次执行时返回null

尝试包含肢体

<table id="user_data" class="table table-bordered table-striped">
    <thead>
     <tr>
      <th>Data</th>
      <th>Resp. de Turno</th>
      <th>Apoio</th>
      <th>Elementos ALA A</th>
      <th>Elementos ALA B</th>
      <th></th>
     </tr>
    </thead>
    <tbody>
    </tbody>
</table>

这将在此处修复您的代码:

$('#add').click(function(){

   var html = '<tr>';
   html += '<td contenteditable id="data1">'+DateIndexer.getNextDate()+'</td>';
   html += '<td contenteditable id="data2"></td>';
   html += '<td contenteditable id="data3"></td>';
   html += '<td contenteditable id="data4"></td>';
   html += '<td contenteditable id="data5"></td>';
   html += '<td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs"><span class="glyphicon glyphicon-send"></span></button></td>';
   html += '</tr>';
   $('#user_data tbody').prepend(html); //<--- right here

});

您的条件也if需要检查,因为第一次执行tbody是空的,请更改为此

class CellDate {
    constructor(start_date) {
        this.date = start_date;
    }
    getNextDate() {
   //here the change for if condition     
   if ($('#user_data tbody tr') != null && $('#user_data tbody tr').length > 0) {
            var ultima_data = $("#user_data tbody tr:first td:first").text().trim().split("-");
            var ultimo_dia = +ultima_data[0];
            var ultimo_mes = +ultima_data[1];
            var ultimo_ano = +ultima_data[2];
            this.date.setDate(ultimo_dia);
            this.date.setMonth(ultimo_mes - 1);
            this.date.setYear(ultimo_ano);
            this.date.setDate(this.date.getDate() + 1);
            console.log('if', this.date);
        } else {
            this.date.setMonth(this.date.getMonth() + 1);
            this.date.setDate(1);
            console.log('else', this.date);
        }
        return novaData(this.date);
    }
}

在这里编码https://jsfiddle.net/nzb0tdjq/

发布
问题

分享
好友

手机
浏览

扫码手机浏览