我的javaScript刚停止工作?

时间:2015-06-23 17:55:01

标签: javascript php jquery

所以我试图创建支票,这样如果有人没有正确输入邮政编码,电话号码或电子邮件地址,那么javascript会显示提醒,表单不会成为提交。如果没有填写任何字段(null || ==""),那么javascript也会显示警告并且不会提交表单。

工作得很好,但突然停止了工作......我现在已经看了一段时间,但无法找出原因。

这是我的代码(.php):

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Adding Companies</title>
<link rel="stylesheet" type="text/css" href="../ResponsiveTopNavDropdown.css" />
<meta name="viewport" content="width=device-width">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="toggleNav.js" type="text/javascript"></script>
</head>

<body>
<div class="container">
    <div class="header"></div>

    <div id="toggleNav"><a href="#" onclick="toggleNav()">Show/Hide Navigation</a></div>

    <nav id="nav">
        <?php include('../nav.html'); ?>
    </nav>


        <h1 id="mainheader">WELCOME TO MISSISSIPPI VALLEY CHAPTER 123</h1>

        <section class="content-wide">
            <?php
                session_start();
                if ($_SESSION['check'] != "true") {
                    header('Location: http://www.mississippivalleyashrae.org/Admin/admin_login.php');
                }
                $_SESSION['my_company'] = null;

            ?>
                <h3><strong>You're currently in the process of: Adding a Company<strong></h3>
                <table width = "760" border = "0" align = "center" cellpadding="0" cellspacing="1" bgcolor = "#cccccc">
                  <tr>
                    <form onSubmit="return validateCompany()" action="adding_manufacturer.php" method="post">
                    <td>
                      <table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#EEEEEE">
                        <tr>
                          <td colspan="3"><strong> Enter the Information for this New Company </strong></td>
                        </tr>
                        <tr>
                          <td colspan="3" align = "left">
                            Company Name<br />
                            <input type="text" name="company_name" id="company_name" placeholder="My Company .Inc"
                                   size="100" maxlength="125"></td>
                        </tr>
                        <tr>
                          <td colspan="3" align = "left">
                            Address<br />
                            <input type="text" name="address" id="address" placeholder="1234 My Street Ave."
                                   size="100" maxlength="100"></td>
                        </tr>
                        <tr>
                          <td align = "left" width="250">
                            City<br />
                            <input type="text" name="city" id="city" placeholder="Rock Island" size="30" maxlength="30"></td>
                          <td align = "left" width="250">
                            State<br />
                            <select name="state">
                                <option value="AL">Alabama</option>
                                <option value="AK">Alaska</option>
                                <option value="AZ">Arizona</option>
                                <option value="AR">Arkansas</option>
                                <option value="CA">California</option>
                                <option value="CO">Colorado</option>
                                <option value="CT">Connecticut</option>
                                <option value="DE">Delaware</option>
                                <option value="DC">District Of Columbia</option>
                                <option value="FL">Florida</option>
                                <option value="GA">Georgia</option>
                                <option value="HI">Hawaii</option>
                                <option value="ID">Idaho</option>
                                <option value="IL">Illinois</option>
                                <option value="IN">Indiana</option>
                                <option value="IA">Iowa</option>
                                <option value="KS">Kansas</option>
                                <option value="KY">Kentucky</option>
                                <option value="LA">Louisiana</option>
                                <option value="ME">Maine</option>
                                <option value="MD">Maryland</option>
                                <option value="MA">Massachusetts</option>
                                <option value="MI">Michigan</option>
                                <option value="MN">Minnesota</option>
                                <option value="MS">Mississippi</option>
                                <option value="MO">Missouri</option>
                                <option value="MT">Montana</option>
                                <option value="NE">Nebraska</option>
                                <option value="NV">Nevada</option>
                                <option value="NH">New Hampshire</option>
                                <option value="NJ">New Jersey</option>
                                <option value="NM">New Mexico</option>
                                <option value="NY">New York</option>
                                <option value="NC">North Carolina</option>
                                <option value="ND">North Dakota</option>
                                <option value="OH">Ohio</option>
                                <option value="OK">Oklahoma</option>
                                <option value="OR">Oregon</option>
                                <option value="PA">Pennsylvania</option>
                                <option value="RI">Rhode Island</option>
                                <option value="SC">South Carolina</option>
                                <option value="SD">South Dakota</option>
                                <option value="TN">Tennessee</option>
                                <option value="TX">Texas</option>
                                <option value="UT">Utah</option>
                                <option value="VT">Vermont</option>
                                <option value="VA">Virginia</option>
                                <option value="WA">Washington</option>
                                <option value="WV">West Virginia</option>
                                <option value="WI">Wisconsin</option>
                                <option value="WY">Wyoming</option>
                            </select></td>
                          <td align = "left" width="250">
                            Zip-Code<br />
                            <input type="text" name="zip" id="zip" placeholder="61201" size="6" maxlength="6"></td>
                        </tr>
                        <tr>
                          <td align = "left" width="250">
                            Phone<br />
                            <input type="text" name="phone" id="phone" placeholder="515-555-0000" size="12" maxlength="12"></td>
                          <td colspan="2" align = "left" width="250">
                            Email Address<br />
                            <input type="text" name="email" id="email" placeholder="myemail24@gmail.com"
                                   size="50" maxlength="50"></td>
                        </tr>
                        <tr>
                          <td colspan="3">
                            <br />
                            <input type ="submit" name="submit" id="mySubmit" value="Add This Company"></td>
                        </tr>
                      </table>
                    </td>
                    </form>
                  </tr>
                </table>
                <p>
                    **Make sure all information is entered correctly!<br />
                    If you enter any information incorrectly you can go back and edit it from the
                    <a href="http://www.mississippivalleyashrae.org/Admin/admin_main.php">Admin Main</a> page!
                </p>
            <script src="formValidators.js" type="text/javascript"></script>
        </section>

这是我的javascript(formValidators.js):

function validateCompany() {
    var checkCompanyName = document.getElementById("company_name").value;
    var checkAddress = document.getElementById("address").value;
    var checkCity = document.getElementById("city").value;
    var checkState = document.getElementById("state").value;
    var checkZip = document.getElementById("zip").value;
    var checkPhone = document.getElementById("phone").value;
    var checkEmail = document.getElementById("email").value;

    // checks for any null fields
    if ((checkCompanyName==null || checkCompanyName=="") || (checkAddress==null || checkAddress=="") ||
    (checkCity==null || checkCity=="") || (checkState==null || checkState=="") ||
    (checkZip==null || checkZip=="")|| (checkPhone==null || checkPhone=="") ||
    (checkEmail==null || checkEmail=="")){
        alert("Please Fill All Fields");
        return false;
    }
    var zipPattern = /^\\d{5}(-\\d{4})?$/;
    if (zipPattern.test(checkZip) == false){
        alert("Invalid Zip-Code, Please Re-Enter the zip-code in the correct format");
        return false;
    }
    var phonePattern = /^()?\d{3}()?(-|\s)?\d{3}(-|\s)\d{4}$/;
    if (phonePattern.test(checkPhone) == false){
        alert("Invalid Phone Number, Please Re-Enter the phone number in the correct format");
        return false;
    }
    var emailPattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if (emailPattern.test(checkEmail) == false){
        alert("Invalid Email, Please Re-Enter the email in the correct format");
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

看起来问题出现在你的状态下拉:

<select name="state">

没有ID可以获取,所以你的JS失败了,我可以默默地添加。

<select name="state" id="state">

我修改了选择器以包含一个ID,现在它正在正常启动。