如何使用此代码获取警报框和验证

时间:2019-05-07 08:30:41

标签: javascript html

我编写了此代码,但我无法通过带有缺失字段的警报框来验证必填字段,或者像我想要的那样提交到弹出窗口。该代码应用于验证某些字段,这些字段仅在单击单选按钮之一后才显示,并且包含URL的弹出窗口应在提交后显示。不工作需要帮助。

代码:

window.onload = function() {
  document.getElementById('ifBusiness').style.display='none';
}

function BusinessorResidence() {
  if(document.getElementById('businessCheck').checked) {
    document.getElementById('ifBusiness').style.display='block';
    document.getElementById('ifResidence').style.display='none';
  }
  else {
    document.getElementById('ifBusiness').style.display='none';
    document.getElementById('ifResidence').style.display='block';
  }
}

function validateForm() {
  var address=document.forms["myForm"]["address"];
  var bname=document.forms["myForm"]["bname"];
  var url=document.forms["myForm"]["url"];
  var id=document.forms["myForm"]["tax"];
  var rname=document.forms["myForm"]["rname"];
  var email=documen.forms["myForm"]["email"];

  if(address.value == "") {
    alert("Please enter an address.");
    address.focus;
    return false;
  }

  if(bname.value == "") {
    alert("Please enter a business name.");
    bname.focus;
    return false;
  }

  if(url.value == "") {
    alert("Please enter a business URL.");
    url.focus;
    return false;
  }

  if(id.value == "") {
    alert("Please enter a business tax ID.");
    id.focus;
    return false;
  }

  if(rname.value == "") {
    alert("Please enter a residence name.");
    rname.focus;
    return false;
  }

  if(email.value == "") {
    alert("Please enter an email address.");
    email.focus;
    return false;
  }

  return true;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Javascript Assignment</title>
    <center><h1>Fill the form below</h1>
  </head>
  <body>
    <form name="myForm" action="http://csit.selu.edu/cgi-bin/echo.cgi" onsubmit="return validateForm()" method="post">
      <center><p><b>Address: </b><input type="text" name="address"></p>
      <div>
      <div>
      <input type="radio" onclick="javascript:BusinessorResidence();" name="businessresidence" id="businessCheck">This is a Business <input type="radio" onclick="javascript:BusinessorResidence();" name="businessresidence" id="residenceChceck">This is a Residence<br>
      <div id="ifBusiness" style="display:none">
      <b>Business Name: </b><input type="text" id="name" name="bname"><br>
      <b>Business Website URL: </b><input type="text" id="url" name="url"><br>
      <b>Business Tax ID: </b><input type="text" id="tax" name="tax"><br>
      <input type="submit" value="Submit">
      </div>
      <div id="ifResidence" style="display:none">
      <b>Name: </b><input type="text" id="name" name="rname"><br>
      <b>Email: </b><input type="text" id="email" name="email"><br>
      <input type="submit" value="Submit"></center>
      </div>
    </form>

    <hr>
    <hr>
    </body>
</html>

警报框没有弹出,我不确定如何解决。

1 个答案:

答案 0 :(得分:0)

根据评论,我添加了是否进行业务从句检查。

我还添加了用于在新窗口中打开表单提交的代码。

最后,我清理了所有您不再需要使用的东西,例如<b><center>之类的过时HTML标签,并在适当时添加了注释。

<!DOCTYPE html>
<html>
  <head>
    <title>
      <h1>Javascript Assignment</h1>
      <!-- the titles should be inside the title, not inside the <head> tag -->
      <h2>Fill the form below</h2>
    </title>
    <!-- center tag is deprecated and should be replaced by CSS -->
    <style>
    head {
      text-align: center;
    }
    .bold {
      font-weight: bold;
    }
    </style>
    <!-- script tags should be at the bottom of the body, not inside the <head> -->
  </head>
  <body>
    <form name="myForm" action="http://csit.selu.edu/cgi-bin/echo.cgi" onsubmit="return validateForm()" method="post">
      <p>
        <span class="bold">Address: </span>
        <input type="text" name="address">
      </p>
      <div>
        <div>
          <input type="radio" onclick="javascript:BusinessorResidence();" name="businessresidence" id="businessCheck">This is a Business
          <input type="radio" onclick="javascript:BusinessorResidence();" name="businessresidence" id="residenceChceck">This is a Residence
          <br>
          <div id="ifBusiness" style="display:none">
            <!-- <b> tag is deprecated. should be done with CSS -->
            <span class="bold">Business Name:</span>
            <input type="text" id="name" name="bname">
            <br>
            <span class="bold">Business Website URL:</span>
            <input type="text" id="url" name="url">
            <br>
            <span class="bold">Business Tax ID: </span>
            <input type="text" id="tax" name="tax">
          </div>
          <div id="ifResidence" style="display:none">
            <span class="bold">Name: </span>
            <input type="text" id="name" name="rname">
            <br>
            <span class="bold">Email: </span>
            <input type="text" id="email" name="email">
          </div>
        <!-- missed the closing </div> -->
        </div>
      </div>
      <!-- Only one submit button per form -->
      <input type="submit" value="Submit">
    </form>
    <hr>
    <hr>
    <!-- script tags should be at the bottom of the body, not inside the <head> -->
    <!-- both scripts can be in the same <script> tag -->
    <script>
    window.onload = function() {
      document.getElementById('ifBusiness').style.display='none';
    }

    function BusinessorResidence() {
      var is_business = document.getElementById('businessCheck').checked;
      if ( is_business ) {
        document.getElementById('ifBusiness').style.display='block';
        document.getElementById('ifResidence').style.display='none';
      }
      else {
        document.getElementById('ifBusiness').style.display='none';
        document.getElementById('ifResidence').style.display='block';
      }
    }

    function validateForm() {
      var is_business = document.getElementById('businessCheck').checked;
      var address = document.forms["myForm"]["address"];
      var bname = document.forms["myForm"]["bname"];
      var url = document.forms["myForm"]["url"];
      var tax = document.forms["myForm"]["tax"];
      var rname = document.forms["myForm"]["rname"];
      var email = document.forms["myForm"]["email"];
      // Address always has to be checked
      if ( address.value == "" ) {
        alert("Please enter an address.");
        address.focus();
        return false;
      }
      // Check the banem, tax and url if a business is selected
      if ( is_business ) {
        if ( bname.value == "" ) {
          alert("Please enter a business name.");
          // focus() is a method, not a property, so you need to call this function to actually focus the text input.
          bname.focus();
          return false;
        }
        // It does not make sense to call the tax id "id", tax is a better name.
        if ( tax.value == "" ) {
          alert("Please enter a business tax ID.");
          tax.focus();
          return false;
        }
        if ( url.value == "" ) {
          alert("Please enter a business URL.");
          url.focus();
          return false;
        }
      }
      // Else check the rname and the email
      else {
        if ( rname.value == "" ) {
          alert("Please enter a residence name.");
          rname.focus();
          return false;
        }
        if ( email.value == "" ) {
          alert("Please enter an email address.");
          email.focus();
          return false;
        }
      }
      // Open the popup window.
      // _blank refers to it being a new window
      // SELU is the name we'll use for the window.
      // The last string is the options we need.
      var popup = window.open( '', 'SELU', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=400,left=312,top=234' );
      // Set the form target to the name of the newly created popup.
      var form = document.querySelector( 'form[name="myForm"]' );
      form.setAttribute( 'target', 'SELU' );
      // Continue as normal
      return true;
    }
    </script>
  </body>
</html>