2012-06-09 98 views
3

我有这样的代码是否存在的代码,检查代码的可用性形式不提交Jquery的form.submit没有工作

我有没有工作

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#Loading').hide(); 
      var min_chars = 3; 
      //result texts 
      var characters_error = 'Minimum amount of chars is 3'; 
      var checking_html = 'Checking...';  


     $('#reality_form').submit(function(){ 

      if($('#realitycode').val().length < min_chars){ 
       $('#Info').html(characters_error); 
      }else{ 
       //else show the cheking_text and run the function to check 
        $('#Info').html(checking_html); 
         var reltcode = $('#realitycode').val(); 

       //use ajax to run the check 
       $.post("http://localhost/Testtt/wp-content/plugins/Realestate Redirection/check_realitycode_availablity.php", { realitycode: reltcode }, 

       function(result){ 
        //if the result is 1 
        if(result == 1){ 
         //show that the username is available 
         alert('hi'); 
         $('#Info').html(reltcode + ' is Available'); 

        }else{ 
         alert('hello'); 
         $('#Info').html(reltcode + ' is not Available'); 

        } 

       }); 
      return false;  
      } 

    }); 

}); 

</script> 
<div class="wrap"> 
      <?php echo "<h2>" . __('RealEstate Listing Options', 'oscimp_trdom') . "</h2>"; ?> 
      <form action="" name="reality" method="post" id="reality_form"> 
      <input type="hidden" name="reality_hidden" value="Y"> 
      Website Url:<input type="text" name="website_url" value="" /> 
      Listing Code: <input type="text" name="rlt_code" id="realitycode" /> 
      <input type="submit" name="submit" value="submit" /> 
      <div id="Info"></div> 
      <span id="Loading"><img src="http://localhost/Testtt/wp-content/plugins/Realestate Redirection/loader.gif" alt="" /></span> 

      </form> 
</div> 

正如我看看代码控制台的Ajax网址会变成错误,但是当我使用realitycode keyup()函数时,则Ajax可以正常工作。如果有任何错误,我希望表单不应该提交。但形式提交在任何情况下 请帮助

+0

您将返回false /真在里面内function.so不能返回提交功能,用于阻止其submmiting假的。 –

+0

那么我该如何防止 – Ankit

+0

我发布了answer.try并检查是否有帮助? –

回答

1

我想表单不应该提交如果有任何错误。但形式是 提交在任何情况下,请帮

return false年底你$('#reality_form').submit()功能

$('#reality_form').submit(function(){ 
    // your other code 
    return false; 
}); 
+0

当我把'code'返回false时,它在每个case中都会停下来; '代码'在函数结束,因为说你可以通过代码显示哪里放 – Ankit

+0

@Ankit:它应该把你的AJAX代码 – Sarfraz

+0

我已经做了这样的 – Ankit

2

使用。点击事件,而不是提交。然后,如果表单有效,请致电以代码形式提交。

0

化妆之后改变了代码:

 <script type="text/javascript"> 
    var validated = false; 
    $(document).ready(function() { 

     $('#Loading').hide(); 
       var min_chars = 3; 
       //result texts 
       var characters_error = 'Minimum amount of chars is 3'; 
       var checking_html = 'Checking...';  


      $('#reality_form').submit(function(){ 
       if(validated) 
        return true; 
       if($('#realitycode').val().length < min_chars){ 
        $('#Info').html(characters_error); 
       }else{ 
        //else show the cheking_text and run the function to check 
         $('#Info').html(checking_html); 
          var reltcode = $('#realitycode').val();     

        //use ajax to run the check 
        $.post("http://localhost/Testtt/wp-content/plugins/Realestate Redirection/check_realitycode_availablity.php", { realitycode: reltcode }, 

        function(result){ 
         //if the result is 1 
         if(result == 1){ 
          //show that the username is available 
          alert('hi'); 
          $('#Info').html(reltcode + ' is Available'); 
          //do nothing 

         }else{ 
          alert('hello'); 
          $('#Info').html(reltcode + ' is not Available'); 
          validated = true; 
          $('#reality_form').submit(); 
         } 
        }); 
        return false;  
       } 

     });  

    }); 

    </script> 
+0

解决方案从此链接推断:http://stackoverflow.com/问题/ 2637985 /停止,然后重启提交 - 后阿贾克斯检查 –

0

其他方式,从提交到防止使用下面的代码:

$('#reality_form').submit(function(e){ 

    e.preventDefault(); 

在jQuery中每一个处理程序接收事件对象作为第一个参数,此对象具有禁止默认行为的方法prevetDefault()。它可以用于任何类型的事件,例如<a>元素上的click

0

我想出了同样的问题。 解决的办法是...

Dont use name="submit" 
<input type="submit" name="submit" value="submit" /> 

Instead 
<input type="submit" name="anyOtherName" value="submit" />