2012-04-13 5 views
0

JQueryコードに問題があり、問題が何であるか分かりません。基本的にはajaxを使用してオンザフライでコメントを送信しようとしていますが、ページがリロードし続ける理由。以下のコードをチェックアウト...Ajaxポスト関数は、動的にデータを送信する代わりにページをリロードする

HTMLフォーム

<form method='post' action=''>      
    <textarea name='comment' id='comment' maxlength='500'>Leave a Comment...</textarea> 
    <input name='userActivityId' id='userActivityId' type='hidden' value='$userid'> 

    <input class='send' id='formButton' type='submit' value='Send'> 
</form> 

jQueryのコード

$(function(){ 

    /*this function submits a normal comment from the comment container*/ 
    $(".commentContainer .send").on('click', function(){  
     var profileId = $("input#userActivityId").val(); 
     var comment = $("textarea#activityComment").val();  

     if(profileId == ""){ 
     $("input#userActivityId").focus(); 
     return false; 
     } 

     if(comment == ""){ 
     $("textarea#comment").focus(); 
     return false; 
     } 

     //send message 
     postActivityMessage(comment, profileId); 

     return; 
    }); 


    function postActivityMessage(comment, toUser){ 
     $.ajax({ 
     type: "POST", url: "include/process.php", 
     data:{ 
      addActivityComment: "true", 
      message: comment, 
      userActivityId: toUser, 
      type: "message", 
      replyto: '0'  
     }, 
     success: function(){ 
      alert('posted'); 
     } 
     }); 
    } 

}); 
+1

あなたごとに、助けるべきですデフォルトのイベント処理をキャンセルしません。また、ボタンのIDを使用してみませんか?それらの全体の束がありますか? –

+0

@dave申し訳ありませんが、どういう意味ですか?フォームアクションについて話していますか? –

+0

はいいいえ静か同じボタンのいくつか –

答えて

1

これは「Daveのコメント

$("form").submit(function(e) { 
    e.preventDefault(); 
}); 
+0

はうまく働いてくれてありがとう –

関連する問題