JavaScript - parseInt和Number无效

时间:2018-03-07 03:57:58

标签: javascript

我正在尝试从字符串(html5输入数据)到整数的简单转换,但我的控制台日志始终返回Undefined,与Numbers()或{{1}的结果相同}。如果我写了一封“信”(仅用于测试,我的表格是ParseInt)。

input="number"
var dia = Number(document.getElementById('dia').value)
var mes = Number(document.getElementById('mes').value)
var ano = Number(document.getElementById('ano').value)

form.addEventListener('submit', function(e) {

  console.log(dia.value)
  console.log(mes.value)
  console.log(ano.value)

  e.preventDefault()

})
    

对不起,如果它是一种跛脚,我发现了很多关于这个问题,但我无法弄清楚如何解决。

3 个答案:

答案 0 :(得分:1)

您必须检索addEventListener函数中的值。由于diamesano已经是值

,因此无需在控制台上使用form.addEventListener('submit', function(e) { var dia = Number(document.getElementById('dia').value); var mes = Number(document.getElementById('mes').value); var ano = Number(document.getElementById('ano').value); console.log(dia) console.log(mes) console.log(ano) e.preventDefault(); })



<form name="form" action="index.html" method="post">
  <div class="">
    <label for="dia">Dia de Nascimento</label>
    <input type="number" name="dia" id="dia">
  </div>
  <div class="">
    <label for="mês">Mês de Nascimento</label>
    <input type="text" name="mes" id="mes">
  </div>
  <div class="">
    <label for="ano">Ano de Nascimento</label>
    <input type="number" name="ano" id="ano">
  </div>
  <div class="">
    <input type="submit" name="" value="Enviar">
  </div>
</form>
&#13;
{{1}}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您应该考虑转换回调方法本身的数字。

form.addEventListener('submit',  function(e) {
  var dia = Number(document.getElementById('dia').value)
  var mes = Number(document.getElementById('mes').value)
  var ano = Number(document.getElementById('ano').value)
  console.log(dia)
  console.log(mes)
  console.log(ano)

  e.preventDefault()

})

这是一个有效的例子:

<html>
  <head>
    <script type="text/javascript">
      window.onload = function(){
        form = document.getElementById("form")
        form.addEventListener('submit',  function(e) {
          var dia = Number(document.getElementById('dia').value);
          var mes = Number(document.getElementById('mes').value);
          var ano = Number(document.getElementById('ano').value);
          console.log(dia);
          console.log(mes);
          console.log(ano);
          e.preventDefault();
        });
      };
    </script>
  </head>
  <body>
    <form name="form" action="index.html" method="post" id="form">
      <div class="">
          <label for="dia">Dia de Nascimento</label>
          <input type="number" name="dia" id="dia">
      </div>
      <div class="">
          <label for="mês">Mês de Nascimento</label>
          <input type="text" name="mes"  id="mes">
      </div>
      <div class="">
          <label for="ano">Ano de Nascimento</label>
          <input type="number" name="ano"  id="ano">
      </div>
      <div class="">
          <input type="submit" name="" value="Enviar">
      </div>
    </form>
  </body>
</html>

答案 2 :(得分:0)

只需更改此功能

即可
    form.addEventListener('submit',  function(e) {

        console.log(dia)
        console.log(mes)
        console.log(ano)
        e.preventDefault()

   })