2015-11-13 51 views
-1

我想驗證表單以查看教科書是否已填充。 它應該提醒你提交後箱子是否被填滿,但我的網頁沒有做任何事情。我不確定會出現什麼問題。Javascript表單驗證 - 文本框檢查

$(document).ready(function() { 
     $('#customerSubmit').click(function(e) { 
      var isValid = true; 
      $('input[type="text"]').each(function() { 
      if ($.trim($(this).val()) == '') { 
       isValid = false; 
       $(this).css({ 
        "border": "1px solid red", 
        "background": "#FFCECE" 
       }); 
      } else { 
       $(this).css({ 
        "border": "", 
        "background": "" 
      }); 
      } 
     }); 
     if (isValid == false) { 
      e.preventDefault(); 
     } else { 
      alert('Thank you for submitting'); 
    }); 
}); 

<!DOCTYPE html> 
<html> 
<head> 
<body> 
    <h3>Contact Information </h3> 
     <table class="customerInfo"> 
      <tr> 
       <th>First Name:</th> 
       <td><input type="text" name="custFirstName"/></td> 
      </tr> 
      <tr> 
       <th>Last Name:</th> 
       <td><input type="text" name="custLastName"/></td> 
      </tr> 
      <div class="control-group" id="controlEmail"> 
       <tr> 
        <th>Email:</th> 
        <td><input type="text" name="custEmail" id="email"/></td> 
       </tr> 
      </div> 
     </table> 
     <input id="customerSubmit" class="submit" type="submit" value="Submit" name="customerSubmit"/> 
</body> 
</html> 
+0

在控制檯上看到什麼錯誤? – void

+0

您的代碼中存在格式問題。修正它在這裏:http://jsfiddle.net/x74h9nfd/ – DinoMyte

+0

你知道你的代碼爲語法錯誤?! –

回答

0

您的代碼最底部的else上缺少一個末端支架。