日期jquery函数无法在表行上工作

时间:2017-10-14 11:10:19

标签: javascript jquery

逻辑首先选择每行的开始日期,然后单击按钮,结果日期将根据跳过日显示。出于某种原因,我的代码没有工作,任何想法?感谢



$(".add").on('click', 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('/'));
}
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/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"><button class="add" type="button">OK</button></td>
      <td><input type="text" value="3" class="day"> </td>
      <td><input type="text" class="result"> </td>
    </tr>

    <tr>
      <td>
        <input type="date" class="date"><button class="add" type="button">OK</button></td>
      <td><input type="text" value="3" class="day"> </td>
      <td><input type="text" class="result"> </td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您还需要额外)var start = $row.find('.date').val());

&#13;
&#13;
$(".add").on('click', 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('/'));
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/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"><button class="add" type="button">OK</button></td>
      <td><input type="text" value="3" class="day"> </td>
      <td><input type="text" class="result"> </td>
    </tr>

    <tr>
      <td>
        <input type="date" class="date"><button class="add" type="button">OK</button></td>
      <td><input type="text" value="3" class="day"> </td>
      <td><input type="text" class="result"> </td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

在此行)

的最后一行删除此var start = $row.find('.date').val());