javascript - 表单 onSubmit 不调用 javascript 函数

标签 javascript

我检查了最近关于 onsubmit 事件不触发该方法的博客。这些建议对这个问题没有帮助。我已经在另一个 html 页面中尝试过这种方法和表单,但也不起作用。所以我无法找出主要问题在哪里? 我的代码:

<div id="_div2">
<center>
<div class="contianer">

<script type="text/javascript">
function formValidation ()
{
    var fName =document.Log.firstName.value;
    var lName =document.Log.lastName.value;
    var pName =document.Log.penName.value;
    var email =document.Log.email.value;
    var password=document.Log.password.value;
    var confirmPassword=document.Log.confirmPassword.value;
    var status=false;
    if(fName.length<1)
    {
        document.getElementById("firstNameLoc").innerHTML=
        "<img src='Resource/unchecked.gif'/>Please Enter your First Name";
        status=false;
    }
    else 
    {
        document.getElementById("firstNameLoc").innerHTML=
        "<img src="Resource/checked.png"/>Please Enter your First Name";
        status=true;
    }
    return status;
}
</script>


<form name="Log" action="SignUpInsert.php" method="post" onsubmit="return formValidation();return false;">
<label><b>First Name</b></label><span id="firstNameLoc"></span><br>
<input type="text" placeholder="Enter your Last Name"  name="firstName"><br>
<label><b>Last Name</b></label><span id="lastNameLoc"></span><br>
<input type="text" placeholder="Enter your Last Name" name="lastName"><br>
<label><b>Pen Name</b></label><span id="penNameLoc"></span><br>
<input type="text" placeholder="Enter your Unique Pen Name" name="penName"><br>
<label><b>Email</b></label><span id="emailLoc"></span><br>
<input type="text" placeholder="Enter your Email" name="email"><br>
<label><b>Password</b></label><span id="passwordLoc"></span><br>
<input type="password" placeholder="Enter your Password" name="password"><br>
<label><b>Confirm Password</b></label><span id="confirmPasswordLoc"></span><br>
<input type="password" placeholder="Enter your Password Again" name="confirmPassword"><br>
<input type="submit" title="Done?" value="Sign Up"><br>

<div class ="contianer">
<button type="button" class="cancelBtn" title="Go Back" onclick="location.href='Main.html'">Cancel</button>
</div>

</form>
</div>
</center>

最佳答案

你快完成了。简单的 quotes 问题。您可以在 dom else 语句中进行这样的更改

document.getElementById("firstNameLoc").innerHTML = '<img src="Resource/checked.png"/>Please Enter your First Name';

function formValidation() {
  var fName = document.Log.firstName.value;
  var lName = document.Log.lastName.value;
  var pName = document.Log.penName.value;
  var email = document.Log.email.value;
  var password = document.Log.password.value;
  var confirmPassword = document.Log.confirmPassword.value;
  var status = false;
  if (fName.length < 1) {
    document.getElementById("firstNameLoc").innerHTML =
      "<img src='Resource/unchecked.gif'/>Please Enter your First Name";
    status = false;
  } else {
    document.getElementById("firstNameLoc").innerHTML = '<img src="Resource/checked.png"/>Done..!';
    status = true;
  }
  return status;
}
<div id="_div2">
  <center>
    <div class="contianer">


      <form name="Log" action="SignUpInsert.php" method="post" onsubmit="return formValidation();return false;">
        <label><b>First Name</b></label><span id="firstNameLoc"></span><br>
        <input type="text" placeholder="Enter your Last Name" name="firstName"><br>
        <label><b>Last Name</b></label><span id="lastNameLoc"></span><br>
        <input type="text" placeholder="Enter your Last Name" name="lastName"><br>
        <label><b>Pen Name</b></label><span id="penNameLoc"></span><br>
        <input type="text" placeholder="Enter your Unique Pen Name" name="penName"><br>
        <label><b>Email</b></label><span id="emailLoc"></span><br>
        <input type="text" placeholder="Enter your Email" name="email"><br>
        <label><b>Password</b></label><span id="passwordLoc"></span><br>
        <input type="password" placeholder="Enter your Password" name="password"><br>
        <label><b>Confirm Password</b></label><span id="confirmPasswordLoc"></span><br>
        <input type="password" placeholder="Enter your Password Again" name="confirmPassword"><br>
        <input type="submit" title="Done?" value="Sign Up"><br>

        <div class="contianer">
          <button type="button" class="cancelBtn" title="Go Back" onclick="location.href='Main.html'">Cancel</button>
        </div>

      </form>
    </div>
  </center>

关于javascript - 表单 onSubmit 不调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43719522/

相关文章:

.net - 有没有办法跟踪 javascript 的命中率?

javascript - 如何在javascript中将PDF文件转换为base64字符串

javascript - 间隔列向谷歌折线图添加左/右填充

javascript - 如何在不刷新页面的情况下更改路由和内容? (机器人友好)

javascript - 在 Google Earth Plug-in API 中将事件附加到 groundOverlay

javascript - 在特定属性值上使用 jQuery 切换(隐藏/显示)<td>

javascript - 在 JavaScript 或 Jquery 中查找 <a> 元素 href#value

javascript - 从数组中的另一个对象更新数组中的对象

javascript - 找不到模块 : can't resolve 'moduleName' in react js

javascript - 如何在 firebase 托管中包含子目录