首页 > 解决方案 > 使用带有 else 的 if 语句来验证表单并在表单有效时重定向到新页面

问题描述

嗨,我有此代码来验证有效的表单,但如果表单有效,它不执行 else 语句并直接到页面

function printpage() {
  if (!mandatoryText(document.getElementById('FIRSTNAME_FIELD'), 'First name')) return false;
  if (!mandatoryText(document.getElementById('LASTNAME_FIELD'), 'Last name')) return false;
  if (!mandatoryText(document.getElementById('ZIP_CODE_FIELD'), 'Post code')) return false;
  if (!mandatoryText(document.getElementById('EMAIL_FIELD'), 'Email')) return false;
  if (!isEmail(document.getElementById('EMAIL_FIELD'))) return false;
  if (!isDateOK(document.getElementById('LAST_SHOWROOM_VISIT_FIELD').value)) return false;
  if (document.getElementById('LAST_SHOWROOM_VISIT_FIELD').value != '') modifyDateFormat(document.getElementById('LAST_SHOWROOM_VISIT_FIELD'));
  document.getElementById('emvForm').submit();
  if (document.getElementById('LAST_SHOWROOM_VISIT_FIELD').value != '') modifyDateFormat(document.getElementById('LAST_SHOWROOM_VISIT_FIELD'));
  else {
    return true;
    var p1 = document.getElementById('SELECTED_PRODUCT_1_FIELD').value;
    window.location.href = "summary.html?" + "p1" + "=" + p1;
  }
}

标签: javascripthtmlformsvalidationif-statement

解决方案


推荐阅读