0

我对一些 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');
         }
      });
   }

});
4

1 回答 1

1

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

$("form").submit(function(e) {
    e.preventDefault();
});
于 2012-04-13T21:14:52.503 回答