HTML输入类型编号 - 如何知道“步骤”功能触发更改事件?

问题描述 投票:0回答:1

我有一个标准的HTML输入文本框,触发“onChange”事件的javascript函数。输入启用了步进功能,按下向上或向下箭头时值变为0.005。我想确定是否通过按箭头键或用户在输入字段中输入值来触发“onChange”事件。我如何区分这两者?

<input type="number" min="0" max="100" step="0.005" />
javascript html reactjs textbox onchange
1个回答
0
投票
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>Test</h1>
    <input id='myfun' type="number" min="0" max="100" step="0.005" onchange="myFunction()" />
    <div id="result"></div>
</body>

<script>
    function myFunction(){
        console.log('My new value : ', document.getElementById('myfun').value);

        var str = document.getElementById('myfun').value
        document.getElementById('result').innerText = str;
    }
</script>
</html>

Check out my code and you would see every time you change the code, it shows the value and all you needs

<input id='myfun' type="number" min="0" max="100" step="0.005" onchange="myFunction()" />
<div id="result"></div>
function myFunction(){
  console.log('My new value : ', document.getElementById('myfun').value);

  var str = document.getElementById('myfun').value
  document.getElementById('result').innerText = str;
}

<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
  </head>

  <body>
    <h1>Test</h1>
    <input id='myfun' type="number" min="0" max="100" step="0.005" onchange="myFunction()" />
    <div id="result"></div>
  </body>

  <script>
    function myFunction() {
      console.log('My new value : ', document.getElementById('myfun').value);

      var str = document.getElementById('myfun').value
      document.getElementById('result').innerText = str;
    }

  </script>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.