【问题标题】:onsubmit = "function( )" on form not workingonsubmit = "function()" 在表单上不起作用
【发布时间】:2021-02-16 08:32:44
【问题描述】:

我正在做表单验证,当我在表单中使用“onsbmit”调用我的函数“validtae()”时,它不起作用。当我单击按钮提交假设发生的验证不起作用我不知道为什么它不起作用,我尝试在 onsubmit 中添加返回仍然不起作用。我尝试在输入标签内使用 onfocus 调用该函数,它可以工作,但是当我单击按钮时我想工作。

// getting inputs

const namee = document.forms['myForm']['name'];
const email = document.forms['myForm']['email'];
const password =document.forms['myForm']['password'];

// getting where the erroe gonna show

const nameerror = document.querySelector('#nameerror');
const emailerror = document.querySelector('#emailerror');
const passworderror = document.querySelector('#passworderror');

// validation function

function validate(){
    if(namee.value == "" ){
        namee.style.border = "2px solid red";
        nameerror.textContent ="name cannot be blank";
        namee.focus();
        return false;
    }
    if(email.value == "" ){
        email.style.border = "2px solid red";
        emailerror.textContent ="email cannot be blank";
        email.focus();
        return false;
    }
    if(password.value == "" ){
        password.style.border = "2px solid red";
        passworderror.textContent ="password cannot be blank";
        password.focus();
        return false;
    }
}
<form name="myForm" method="post" onsubmit="return validate()">
            <input type="text" id="name" name="name" placeholder="Name" required>
            <div id="nameerror"></div>
             <input type="email" id="email" name="email" placeholder="Email" required>
            <div id="emailerror"></div>
            <input type="password" id="password" name="password" placeholder="Create a password" 
             required>
                <button id="submit" type="submit">Sign Up</button>
 </form>

【问题讨论】:

  • 我的直觉是浏览器自己对required 属性的验证在调用onsubmit 之前就开始了。如果这不是您想要的,请删除 required 属性。
  • 恕我直言,您应该完全重新考虑您的验证 JS。此外,在 HTML 代码中没有 passworderror div.
  • 只需将noValidate 添加到您的表单中,如下所示:&lt;form name="myForm" method="post" onsubmit="return validate()" noValidate&gt;

标签: javascript


【解决方案1】:

我假设您想知道为什么没有显示错误消息。这可能是因为浏览器对required 属性的自己的验证 在调用onsubmit 之前启动。 IE。 validate 目前甚至没有被调用,您可以通过添加 console.log 调用来轻松验证。

如果这不是您想要的,请删除 required 属性。

// getting inputs

const namee = document.forms['myForm']['name'];
const email = document.forms['myForm']['email'];
const password =document.forms['myForm']['password'];

// getting where the erroe gonna show

const nameerror = document.querySelector('#nameerror');
const emailerror = document.querySelector('#emailerror');
const passworderror = document.querySelector('#passworderror');

// validation function

function validate(){
    if(namee.value == "" ){
        namee.style.border = "2px solid red";
        nameerror.textContent ="name cannot be blank";
        namee.focus();
        return false;
    }
    if(email.value == "" ){
        email.style.border = "2px solid red";
        emailerror.textContent ="email cannot be blank";
        email.focus();
        return false;
    }
    if(password.value == "" ){
        password.style.border = "2px solid red";
        passworderror.textContent ="password cannot be blank";
        password.focus();
        return false;
    }
}
<form name="myForm" method="post" onsubmit="return validate()">
  <input type="text" id="name" name="name" placeholder="Name">
  <div id="nameerror"></div>
  <input type="email" id="email" name="email" placeholder="Email">
  <div id="emailerror"></div>
  <input type="password" id="password" name="password" placeholder="Create a password" >
  <button id="submit" type="submit">Sign Up</button>
</form>

或者让浏览器处理验证并删除您自己的validate 函数。

【讨论】:

    【解决方案2】:

    如果任何验证检查失败,您应该使用event.preventDefault()

    function validate(event){
        let invalid = 0
        if (namee.value == "" ) {
            namee.style.border = "2px solid red";
            nameerror.textContent ="name cannot be blank";
            namee.focus();
            invalid++;
        }
        if (email.value == "" ){
            email.style.border = "2px solid red";
            emailerror.textContent ="email cannot be blank";
            email.focus();
            invalid++;
        }
        if (password.value == "" ){
            password.style.border = "2px solid red";
            passworderror.textContent ="password cannot be blank";
            password.focus();
            invalid++;
        }
        if (invalid > 0) {
            event.preventDefault()
            return false
        }
    }
    

    【讨论】:

      【解决方案3】:
      看一下这个
          <input type="text" id="name" name="name" placeholder="Name" required>
              <div id="nameerror"></div>
               <input type="email" id="email" name="email" placeholder="Email" required>
              <div id="emailerror"></div>
              <input type="password" id="password" name="password" placeholder="Create a password" 
               required>
                  <button onsubmit="validate()" id="submit" type="submit">Sign Up</button>
      

      【讨论】:

      • 你在 html 中调用一个函数而不是返回其中的函数,你已经在上面的 JavaScript 代码中返回了你的函数
      • submit 事件不会在button 上触发。
      • 然后用输入标签代替按钮标签
      【解决方案4】:
      <input type="submit" value="submit"/>
      

      把提交按钮改成输入,就可以了。

      【讨论】:

      • 使用按钮有什么问题?
      猜你喜欢
      • 2015-11-11
      • 2018-08-06
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      相关资源
      最近更新 更多