jquery提交表单和javascript验证

时间:2014-01-30 11:59:17

标签: javascript jquery submit

大家好,

我有这段代码:

$(document).ready(function() {
   $('#form1').submit(function() {  
        $.post("http://thisismyurl.php",$(this).serialize(),function(data){
         window.location = "reply.html?reply=1";                     
   });
   return false; 
   });
});

我想执行它,如果验证没问题,验证是javascript,并且像这样:

    function validar() 
    {
       if (form1.one.checked == false && form1.two.checked == false &&  form1.three == false)
       {
          alert ('you must choose at least one option');
          return false;
       } 
       else if (form1.four.checked == false && form1.five.checked == false && form1.six.checked == false) 
       {
          alert ('you must choose at least one option for etc');
          return false;

       } 
       else 
       {  
          return true;
       }
     }

我怎样才能做到这一点?提前谢谢!!

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

  $('#form1').submit(function() {
     if(validat()) {
        $.post("http://thisismyurl.php",$(this).serialize(),function(data){
           window.location = "reply.html?reply=1";                     
        });
     }
     else {
        return false; 
     }
 });

答案 1 :(得分:0)

$('#form1').submit(function(e) {
    e.preventDefault();
    if ( !validar() ) return false;
    $.post("http://thisismyurl.php",$(this).serialize(),function(data){
        window.location = "reply.html?reply=1";                     
    });
});