简体   繁体   English

如何摆脱NaN / NaN / NaN

[英]How to get rid of the NaN/NaN/NaN

In the table, for the skips day column, the last row's default value will always be the word "last" which isn't a number. 在表中,对于“跳过日”列,最后一行的默认值将始终是单词“ last”(不是数字)。 Now, the result date show "NaN/NaN/NaN",is there any way that i can replace that with sth like Nil. 现在,结果日期显示为“ NaN / NaN / NaN”,有什么办法可以用Nil代替。

Many thanks. 非常感谢。

  $('input.date, input.day').on('change', function () { var $row = $(this).closest('tr'); var start = $row.find('.date').val(); if (start) { var set = new Date(start); set.setDate(set.getDate() + Number($row.find(".day").val())); $row.find(".result").val([set.getMonth() + 1, set.getDate(), set.getFullYear()].join('/')); var dt = set.getFullYear() + "-" + ("0" + (set.getMonth() + 1)).slice(-2) + "-" + ("0" + set.getDate()).slice(-2); $row.next('tr').find('.date').attr('value', dt).trigger('change'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="one"> <th>Date</th> <th>Skip days</th> <th>Result</th> <tbody> <tr> <td><input type="date" class="date"></td> <td><input type="text" value="10" class="day"> </td> <td><input type="text" class="result"> </td> </tr> <tr> <td><input type="date" class="date"></td> <td><input type="text" value="15" class="day"> </td> <td><input type="text" class="result"> </td> </tr> <tr> <td><input type="date" class="date"></td> <td><input type="text" value="last" class="day"> </td> <td><input type="text" class="result"> </td> </tr> </tbody> </table> 

Here you go with a solution 在这里你有一个解决方案

 $('input.date, input.day').on('change', function () { var $row = $(this).closest('tr'); var start = $row.find('.date').val(); if (!isNaN($row.find(".day").val()) && start) { var set = new Date(start); set.setDate(set.getDate() + Number($row.find(".day").val())); $row.find(".result").val([set.getMonth() + 1, set.getDate(), set.getFullYear()].join('/')); var dt = set.getFullYear() + "-" + ("0" + (set.getMonth() + 1)).slice(-2) + "-" + ("0" + set.getDate()).slice(-2); $row.next('tr').find('.date').attr('value', dt).trigger('change'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="one"> <th>Date</th> <th>Skip days</th> <th>Result</th> <tbody> <tr> <td><input type="date" class="date"></td> <td><input type="text" value="10" class="day"> </td> <td><input type="text" class="result"> </td> </tr> <tr> <td><input type="date" class="date"></td> <td><input type="text" value="15" class="day"> </td> <td><input type="text" class="result"> </td> </tr> <tr> <td><input type="date" class="date"></td> <td><input type="text" value="last" class="day"> </td> <td><input type="text" class="result"> </td> </tr> </tbody> </table> 

I've used iSNaN to check the input.day value 我已经使用iSNaN来检查input.day

Hope this will help you. 希望这会帮助你。

You could always just manually update the last cell to overwrite the Null value with something like: $("#one tbody tr:last-of-type .result")[0].value = 'Nil' . 您始终可以手动更新最后一个单元格,以使用以下方式覆盖Null值: $("#one tbody tr:last-of-type .result")[0].value = 'Nil'

This can be seen in the following example: 在以下示例中可以看到:

 $('input.date, input.day').on('change', function() { var $row = $(this).closest('tr'); var start = $row.find('.date').val(); if (start) { var set = new Date(start); set.setDate(set.getDate() + Number($row.find(".day").val())); $row.find(".result").val([set.getMonth() + 1, set.getDate(), set.getFullYear() ].join('/')); var dt = set.getFullYear() + "-" + ("0" + (set.getMonth() + 1)).slice(-2) + "-" + ("0" + set.getDate()).slice(-2); $row.next('tr').find('.date').attr('value', dt).trigger('change'); } $("#one tbody tr:last-of-type .result")[0].value = 'Nil'; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <table id="one"> <th>Date</th> <th>Skip days</th> <th>Result</th> <tbody> <tr> <td> <input type="date" class="date"></td> <td><input type="text" value="10" class="day"> </td> <td><input type="text" class="result"> </td> </tr> <tr> <td> <input type="date" class="date"></td> <td><input type="text" value="15" class="day"> </td> <td><input type="text" class="result"> </td> </tr> <tr> <td> <input type="date" class="date"></td> <td><input type="text" value="last" class="day"> </td> <td><input type="text" class="result"> </td> </tr> </tbody> </table> 

Hope this helps! 希望这可以帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM