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



我在使用一些 JQuery 代码时遇到了一些问题,我不知道问题是什么,基本上我试图使用 ajax 即时提交评论,但由于某种原因页面不断重新加载。 查看下面的代码...

网页表单

<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 Code

$(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');
         }
      });
   }
});

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

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

相关内容

最新更新