【问题标题】:making a unit converter by javascript and html [duplicate]通过javascript和html制作单位转换器[重复]
【发布时间】:2020-07-28 03:58:47
【问题描述】:

function convertion() {
  let inputValue = number(document.getElementById('firstValue').value);
  let result = document.getElementById('secondValue');
  result.innerHTML = inputValue * 10;
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width,initial-scale=1" />

</head>

<body>

  <main>
    <label for="firstValue">dm: </label>
    <input id="firstValue" type='number'>
    <button onclick="convertion()">
  CONVERT
  </button>
    <br/>
    <br/>
    <label for="secondValue">cm: </label>
    <input id="secondValue" readonly>
  </main>
</body>

</html>
这是一个 dm-cm 转换器,我不知道为什么它不起作用。我认为脚本中有错误。 我也不知道如何在这一行中使用 number 函数:
let inputValue = number(document.getElementById('firstValue').value)

【问题讨论】:

  • 我喜欢有人用不相关的线程标记为重复
  • MostafaShaker,你需要注意错误。尝试运行您提供的代码 sn-p,您会得到一个 - sn-p 引擎会为您提供它,但您需要注意它们的开发时间。这就是为什么它像@J那样关闭。 Gandra - 真正的问题是,如何将我的字符串更改为数字。
  • 他的错误是尝试使用innerHTML 访问输入的值,就像@DCR 指出的那样。无需将数字更改为字符串。

标签: javascript html


【解决方案1】:

输入框没有 innerHTML 的使用价值

function convertion() {
  let inputValue = document.getElementById('firstValue').value;
  console.log(inputValue);
  let result = document.getElementById('secondValue');
  result.value = inputValue * 10;
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width,initial-scale=1" />

</head>

<body>

  <main>
    <label for="firstValue">dm: </label>
    <input id="firstValue" type='number' onchange="convertion()">
    
  CONVERT
  </button>
    <br/>
    <br/>
    <label for="secondValue">cm: </label>
    <input id="secondValue" readonly>
  </main>
</body>

</html>

【讨论】:

  • 感谢您的回答。但是如果我想让它在没有按钮的情况下自动转换呢
  • 然后删除按钮并在第一个输入上使用 onchange。 sn-p 更新
猜你喜欢
  • 2016-03-30
  • 1970-01-01
  • 2013-09-04
  • 2016-11-12
  • 2012-04-12
  • 1970-01-01
  • 2020-05-05
  • 2013-04-30
  • 2016-08-14
相关资源
最近更新 更多