我在这里进行表单验证时遇到困难:

// JavaScript Document
$(document).ready( function() {

 $("#submit").click(function(){

  // set initial error value to false
  var hasError = false;

  // set var for each form field
  var order_ref = $("#order_ref").val();
  var order_name = $("#order_name").val();
  var order_telephone = $("#order_telephone").val();
  var order_email = $("#order_email").val();
  var order_message = $("#order_message").val();
  var order_size = $("#sendEmail input.radio:checked").val();

  // validate each of them
  if(order_ref == '') { $("#order_ref").addClass('haserror'); hasError = true; }
  if(order_name == '') { $("#order_name").addClass('haserror'); hasError = true; }
  if(order_telephone == '') {  $("#order_telephone").addClass('haserror'); hasError = true; }
  if(order_email == '') { $("#order_email").val().addClass('haserror'); hasError = true; }
  //if(order_message == '') { $("#order_message").val().addClass('haserror'); hasError = true; }
  if(order_size == '') { $("#order_size").val().addClass('haserror'); hasError = true; }

  // if there are errors
  if(hasError == true) {
   alert('whoa!');
   return false;
  };

  // if there's no errors, proceed
  if(hasError == false) {
   $.post("/photo/theme/foodphoto/includes/mail_send.php",
       {
     // pass each of the form values to the PHP file for processing
     order_ref: order_ref,
     order_name: order_name,
     order_telephone: order_telephone,
     order_email: order_email,
     order_message: order_message,
     order_size: order_size
    },
        function(data){
      // no errors? great now do what you want to show the user his message is sent
      $('#sendEmail, .faded').slideUp(500);
      $('.thanks').delay(500).fadeIn(500);
        }
     );
  };

  return false;
 });

});


你能发现我在做什么错吗?

谢谢! :)

最佳答案

这些行是错误的:

if(order_email == '') { $("#order_email").val().addClass('haserror'); hasError = true; }
if(order_size == '') { $("#order_size").val().addClass('haserror'); hasError = true; }


删除val()

复制/粘贴错误看起来像:)

关于jquery - jQuery表单验证不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4212937/

10-10 03:25