无法从 html javascript 中的输入类型“电子邮件”字段获取值

我想从“电子邮件”类型的输入字段中获取输入值,但我得到的是 NaN。value.password正在工作,我能够获得该password领域的价值,但document.getElementById('em-id').value正在给我NaN.


请帮我解决这个问题。


我的代码片段如下


      <div class="email-div">

        <i class="fas fa-user-shield"></i>

        <input type="email" name="email" id="em-id" placeholder="Email" min="3" max="15" 

        spellcheck="false" required="true">

      </div>

     <div class="pass-div">

       <i class="fas fa-lock"></i>

       <input type="password" name="password" id="pass" placeholder="Password" min="8" max="32" 

       spellcheck="false" required="true">

     </div>

     <div class="btn-div">

       <button class="btn" id="login-btn" v-on:click="login">LOGIN</button>

     </div>





    login: function() {

        let email = document.getElementById('em-id').value

        let password = document.getElementById('pass').value

        if(email && password) {

          console.log('email => ', + email)       //gives output Nan 

          console.log('password => ' + password)

       }

    }


慕标5832272
浏览 59回答 3
3回答

收到一只叮咚

这个逗号和加号造成了这个问题。由于电子邮件前的加号,Javascript 尝试将电子邮件转换为数字类型转换。您应该使用逗号来记录 2 个值,或者不带逗号的 + 来记录单个字符串。, +&nbsp; &nbsp;login: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let email = document.getElementById('em-id').value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let password = document.getElementById('pass').value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(email && password) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('email => '+ email)&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('password => '+ password)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }

慕村225694

em-id 用于类型为电子邮件的输入字段。.Value 期望它是一个数字字段。您可以删除值,代码应该可以工作。

跃然一笑

问题是这里的逗号console.log('email => ', + email)login: function() {&nbsp; &nbsp; &nbsp; &nbsp; let email = document.getElementById('em-id').value&nbsp; &nbsp; &nbsp; &nbsp; let password = document.getElementById('pass').value&nbsp; &nbsp; &nbsp; &nbsp; if(email && password) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('email => ' + email)&nbsp; &nbsp; &nbsp; &nbsp;//gives output Nan&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('password => ' + password)&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript