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

送信を防ぐもう 1 つの方法は、次のコードを使用することです。

$('#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 に答える