簡體   English   中英

未捕獲的類型錯誤:無法在 Javascript 中讀取 null 的屬性“重置”

[英]Uncaught TypeError: Cannot read property 'reset' of null in Javascript

我正在嘗試通過單擊按鈕在表單輸入中驗證電子郵件。 這是代碼:

function ValidateEmail(inputText) {
  var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  if (inputText.value.match(mailformat)) {
    document.form1.coemail.focus();
    return true;
  } else {
    alert('You have entered an invalid email address!Enter again');
    document.getElementById('form1').reset();
    return false;
  }
}

<button class="btn btn-primary nextBtn btn-lg pull-right" type="button"onClick="ValidateEmail(document.form1.coemail)">Next</button>

單擊“下一步”按鈕時,我會正確收到一條警報消息,但之后它會重定向到下一頁,而不是重置當前頁面。 在 chrome 的控制台中檢查元素時,我發現了這個錯誤: Uncaught TypeError: Cannot read property 'reset' of null

我哪里錯了?

id添加到您的表單中:

<form id="form1" role="form" name="form1" action="reg_employer.php" method="post">

或者

嘗試:

function ValidateEmail(inputText)
{
  var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  if(inputText.value.match(mailformat))
{
  document.form1.coemail.focus();
  return true;

}
else
{
  alert("You have entered an invalid email address!Enter again");
  document.form1.reset();   //<== change this line, use form name
  return false;
}
}

顯然我沒有表單ID

<form id="form1" role="form" name="form1" action="reg_employer.php" method="post">

添加表單 ID 解決了該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM