Ajax post函数重新加载页面而不是动态发送数据

时间:2012-04-13 21:08:16

标签: jquery html ajax forms

我有一些JQuery代码的问题,我不知道是什么问题,基本上我试图使用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 个答案:

答案 0 :(得分:1)

根据Dave的评论,这应该会有所帮助

$("form").submit(function(e) {
    e.preventDefault();
});
相关问题