3

描述: 我有:(在表中)

  • 2 列要输入的数据:

    a) 开始,

    b) 结束。(信息:结束必须大于开始才能正常工作)

  • 2 列的数据是计算出来的 / 是常数:

    a) 实际(结束-开始),

    b) 规范(const)

  • 2 列包含我要计算的数据:

    a) 盈余,

    b) 不足

代码操作:( 现在它正在工作)

  • 用户输入以下字段:

    a) 开始

    b) 结束(必须大于开始)[跳过]

  • 计算字段

    a) 实际 = 结束 - 开始。

  • 字段是 conts。

    a) 规范性

我试图实现的目标:

  • 填写以下字段:

    a)剩余(如何?=>剩余=实际规范)(如果实际>规范)

    b) 不足(如何?=> 不足=规范-实际)(如果实际<规范)

我尝试了什么?:

function diff (start, end) {
  start = start.split(":");
  end = end.split(":");
  const startDate = new Date(0, 0, 0, start[0], start[1], 0);
  const endDate = new Date(0, 0, 0, end[0], end[1], 0);
  let diff = endDate.getTime() - startDate.getTime();
  const hours = Math.floor(diff / 1000 / 60 / 60);
  diff -= hours * 1000 * 60 * 60;
  const minutes = Math.floor(diff / 1000 / 60);
  return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}
document.querySelector('table').addEventListener('change', function(e) {
  const classList = e.target.classList
  if (classList.contains('start') || classList.contains('end')) {
    //retrieve the associated inputs
    const tr = e.target.parentNode.parentNode
    const [start, end, actual, normative, surplus, deficiency] = [...tr.querySelectorAll('.start,.end,.actual,.normative,.surplus,.deficiency')]
    const value = diff(start.value, end.value)
    actual.value = value
    
  }
})
// I tried:
/*    <-----------------------------comment
  if (actual.value > normative.value)
  {
  const value_an = diff_act_nor()
  surplus.value = value_an
  } 
  else if (actual.value < normative.value)
  {
  const value_na = diff_nor_act()
  deficiency.value = value_na
  }
  else{
  // do nothing
  }
 }
 
 function diff_nor_act (actual, normative) {
  actual = actual.split(":");
  normative = normative.split(":");
  const actualDate = new Date(0, 0, 0, actual[0], actual[1], 0);
  const normativeDate = new Date(0, 0, 0, normative[0], normative[1], 0);
  let diff = normativeDate.getTime() - actualDate.getTime();
  const hours = Math.floor(diff / 1000 / 60 / 60);
  diff -= hours * 1000 * 60 * 60;
  const minutes = Math.floor(diff / 1000 / 60);
  return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
  
  
 function diff_act_nor (actual, normative) {
  actual = actual.split(":");
  normative = normative.split(":");
  const actualDate = new Date(0, 0, 0, actual[0], actual[1], 0);
  const normativeDate = new Date(0, 0, 0, normative[0], normative[1], 0);
  let diff =  actualDate.getTime() - normativeDate.getTime();
  const hours = Math.floor(diff / 1000 / 60 / 60);
  diff -= hours * 1000 * 60 * 60;
  const minutes = Math.floor(diff / 1000 / 60);
  return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
    ---comment------------->  */
   
<table>
<thead>
<tr>
<th>start</th>
<th>end</th>
<th>actual</th>
<th>normative</th>
<th>surplus</th>
<th>deficiency</th>
</tr>
</thead>
<tbody>
<tr class="day"> 
  <td><input type="time" class="start" id="start_1" value="08:00"></td>
  <td><input type="time" class="end"  id="end_1" value="15:00"></td>
  <td><input type="time" class="actual"  id="actual_1" value="07:00" readonly></td>
   <td><input type="time" class="normative" id="normative_1" value="08:00" readonly></td>
   <td><input type="time" class="surplus" id="surplus_1" value="00:00" readonly></td>
   <td><input type="time" class="deficiency" id="deficiency_1" value="00:00" readonly></td>
</tr>

<tr class="day">
  <td><input type="time" class="start"  id="start_2" value="08:00"></td>
  <td><input type="time" class="end" id="end_2" value="17:00"></td>
  <td><input type="time" class="actual" id="actual_2" value="09:00" readonly></td>
   <td><input type="time" class="normative" id="normative_2" value="08:00" readonly></td>
   <td><input type="time" class="surplus" id="surplus_2" value="00:00" readonly></td>
   <td><input type="time" class="deficiency" id="deficiency_2" value="00:00" readonly></td>
</tr>
</tbody>


</table>

注释代码

a) 注释代码不起作用。

b)取消注释后,它会破坏工作代码,现在可以正常工作

笔记:

a)给定的“值”是随机的,输入以更好地说明情况

4

1 回答 1

0

不确定这个答案是否仍然相关,但我已经设法解决了你的问题。

首先,diff_nor_actanddiff_act_nor函数没有}结束标记。接下来, if-else 子句的格式不正确,最后一个 else 子句的末尾if (actual.value > normative.value) {有一个多余的。}第三,您没有为diff_act_nor()and的参数提供变量diff_nor_act()。最后,该if (actual.value > normative.value) {子句应放在document.querySelector('table').... 我已经解决了下面这个小提琴中的所有问题:

document.querySelector('table').addEventListener('change', function(e) {
  const classList = e.target.classList;
  if (classList.contains('start') || classList.contains('end')) {
    //retrieve the associated inputs
    const tr = e.target.parentNode.parentNode;
    const [start, end, actual, normative, surplus, deficiency] = [...tr.querySelectorAll('.start,.end,.actual,.normative,.surplus,.deficiency')];
    const value = diff(start.value, end.value);
    actual.value = value;
    const norm = normative.value;

    if (value > norm) {
      const value_an = diff_act_nor(value, norm)
      surplus.value = value_an;
    } else if (value < norm) {
      const value_na = diff_nor_act(value, norm)
      deficiency.value = value_na;
    } else {
      // do nothing
    }

  }
});


function diff(start, end) {
  start = start.split(":");
  end = end.split(":");
  const startDate = new Date(0, 0, 0, start[0], start[1], 0);
  const endDate = new Date(0, 0, 0, end[0], end[1], 0);
  let diff = endDate.getTime() - startDate.getTime();
  const hours = Math.floor(diff / 1000 / 60 / 60);
  diff -= hours * 1000 * 60 * 60;
  const minutes = Math.floor(diff / 1000 / 60);
  return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}

function diff_nor_act(actual, normative) {
  actual = actual.split(":");
  normative = normative.split(":");
  const actualDate = new Date(0, 0, 0, actual[0], actual[1], 0);
  const normativeDate = new Date(0, 0, 0, normative[0], normative[1], 0);
  let diff = normativeDate.getTime() - actualDate.getTime();
  const hours = Math.floor(diff / 1000 / 60 / 60);
  diff -= hours * 1000 * 60 * 60;
  const minutes = Math.floor(diff / 1000 / 60);
  return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}

function diff_act_nor(actual, normative) {
  actual = actual.split(":");
  normative = normative.split(":");
  const actualDate = new Date(0, 0, 0, actual[0], actual[1], 0);
  const normativeDate = new Date(0, 0, 0, normative[0], normative[1], 0);
  let diff = actualDate.getTime() - normativeDate.getTime();
  const hours = Math.floor(diff / 1000 / 60 / 60);
  diff -= hours * 1000 * 60 * 60;
  const minutes = Math.floor(diff / 1000 / 60);
  return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}
<table>
<thead>
<tr>
<th>start</th>
<th>end</th>
<th>actual</th>
<th>normative</th>
<th>surplus</th>
<th>deficiency</th>
</tr>
</thead>
<tbody>
<tr class="day"> 
  <td><input type="time" class="start" id="start_1" value="08:00"></td>
  <td><input type="time" class="end"  id="end_1" value="15:00"></td>
  <td><input type="time" class="actual"  id="actual_1" value="07:00" readonly></td>
   <td><input type="time" class="normative" id="normative_1" value="08:00" readonly></td>
   <td><input type="time" class="surplus" id="surplus_1" value="00:00" readonly></td>
   <td><input type="time" class="deficiency" id="deficiency_1" value="00:00" readonly></td>
</tr>

<tr class="day">
  <td><input type="time" class="start"  id="start_2" value="08:00"></td>
  <td><input type="time" class="end" id="end_2" value="17:00"></td>
  <td><input type="time" class="actual" id="actual_2" value="09:00" readonly></td>
   <td><input type="time" class="normative" id="normative_2" value="08:00" readonly></td>
   <td><input type="time" class="surplus" id="surplus_2" value="00:00" readonly></td>
   <td><input type="time" class="deficiency" id="deficiency_2" value="00:00" readonly></td>
</tr>
</tbody>


</table>

我希望这仍然可以帮助你!

于 2020-04-28T14:01:46.487 回答