2015-11-13 52 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上缺少一个末端支架。