Javascript document.getElementById()。值截断秒数(如果为0)

Javascript document.getElementById()。值截断秒数(如果为0),javascript,truncation,Javascript,Truncation,我是一名javascript新手,在以下方面遇到了一些问题: 函数日志\u时间(){ 让时间=document.getElementById(“时间”).value; console.log(时间); } 像这样的东西怎么样 const timeElement = document.getElementById("time") timeElement.addEventListener('change', (e => { // extract e.target.v

我是一名javascript新手,在以下方面遇到了一些问题:

函数日志\u时间(){
让时间=document.getElementById(“时间”).value;
console.log(时间);
}

像这样的东西怎么样

const timeElement = document.getElementById("time")

timeElement.addEventListener('change', (e => {
  // extract e.target.value as timeVal with default value "00:00:00"
  const {target: {value: timeVal = "00:00:00"} = {}} = e || {};

  // extract hours, minutes, seconds from the split array.
  const timeValArray = timeVal.split(":");
  const [hours, minutes, seconds = "00"] = timeValArray;

  console.log("hours: ", hours)
  console.log("minutes: ", minutes)
  console.log("seconds: ", seconds)
}
));
输出:

hours:  17
minutes:  13
seconds:  00

没有足够的上下文来验证或解释您所描述的内容。如果它是一个简单的文本输入字段,那么这种行为就不会发生。请将与问题相关的所有代码(在本例中,我们需要知道id为“time”的元素的HTML)显示为。您可以使用堆栈片段(编辑器工具栏中的图标看起来像
)在这里创建一个关于堆栈溢出的可运行示例。我已经添加了元素的HTML。这就足够了吗?时间的表达式HH:MM与HH:MM:00完全相同。如果你需要零,你可以在你自己的代码中添加它们。问题是这个字段是用户输入的,那么我如何检查它是否是00,当它是00时,我如何将它们添加到代码中?
const{target:{value:timeVal=“00:00:00”}={}}=e|124;{}
是const timeVal=e.target.value | | |“00:00:00”的缩写但在e或目标未定义的情况下也给出默认值。