3

我有这样的代码,如果代码存在表单不提交,它会检查代码可用性

我的代码不起作用

<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 url 变为错误,但是当我使用realitycode keyup()函数时,Ajax 可以正常工作。如果有任何错误,我希望表单不应该提交。但表格在每种情况下都提交请帮助

4

5 回答 5

2

使用 .click 事件而不是提交。然后,如果表单有效,请在代码中调用提交自己。

于 2012-06-09T09:43:50.780 回答
1

如果有任何错误,我希望表单不应该提交。但表格在每种情况下都提交请帮助

放在函数return false末尾$('#reality_form').submit()

$('#reality_form').submit(function(){
   // your other code
   return false;
});
于 2012-06-09T09:40:31.650 回答
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>
于 2012-06-09T09:46:19.773 回答
0

防止提交的另一种方法是使用以下代码:

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

    e.preventDefault();

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

于 2012-06-09T10:31:38.660 回答
0

我想出了同样的问题。解决方案是...

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

Instead
<input type="submit" name="anyOtherName" value="submit" />
于 2014-07-25T11:44:05.947 回答