2014-11-14 43 views
0

我正在使用jquery验证插件来验证表单。如果所有字段都保留空白,它正在工作。但是,如果我在输入字段中输入了一个文件并留空了其他必填字段,则不会再验证其他元素,而只是将表单提交给服务器。jquery验证插件不与文件一起工作

<script> 
$().ready(function() { 
    $("#form1").validate({ 
     submitHandler: function (form) { 
      $.ajax({ 
       type: $(form).attr('method'), 
       url: $(form).attr('action'), 
       data: $(form).serialize(), 
      }) 
      .done(function (response) { 
       jAlert(response);  
      }); 
      return false; 
     }, 
     ignore: [], 
     rules: { 
      title: { required: true, minlength: 2, maxlength: 25 }, 
      text1: { 
       required: function() { 
        CKEDITOR.instances.text1.updateElement(); 
       } 
      }, 
      text2: { 
       required: function() { 
        CKEDITOR.instances.text2.updateElement(); 
       } 
      }, 
      newsimage: { 
       required: true, 
       accept: "image/*" 
      }, 
     }, 
     messages: { 
      title: { 
       required: "Please enter the Title", 
       minlength: "Title must consist of at least 2 characters" 
      }, 
      text1: { 
       required: "Please enter text", 
       minlength: "Must consist of at least 2 characters" 
      }, 
      text2: { 
       required: "Please enter text", 
       minlength: "Must consist of at least 2 characters" 
      } 
     } 
    }); 
}); 
</script> 

这里的形式

<form id="Form1" enctype="multipart/form-data" method="post" action="save.php" > 

    Title: <input name="title" size="40" maxlength="255"> 
    <br> 
    <label for="newsimage">News Image</label> 
    <input type="file" id="newsimage" name="newsimage"> 

    <br> News Summary: 
    <textarea id="text1" name="text1" rows="7" cols="30"></textarea> 

    <script type="text/javascript"> 
      CKEDITOR.replace('text1'); 
    </script> 
    <br> News Full Story: 
    <textarea id="text2" name="text2" rows="7" cols="30"></textarea> 
    <script type="text/javascript"> 
      CKEDITOR.replace('text2'); 
    </script> 
    <br> <input type="submit" name="submit" value="Add News"> 

+0

似乎罚款http://jsfiddle.net/arunpjohny/hwfkn0v7/2/ - 你需要从'required'返回TRUE;回调方法 –

+0

我的代码有问题,它不工作,删除ckeditor,甚至复制你放在jsfiddle中的东西,但它仍然是同样的问题。 –

回答

0

你可以使用需要你的表单标签是这样的:

<input name="title" size="40" maxlength="255" required> 

应该迫使一些输入。 (它没有被消毒)

0

这不是我的最终目标,因为我缺少additional-methods.js,因此添加它可以解决问题。

<script src="js/additional-methods.min.js"></script> 
相关问题