2009-08-11 68 views
0

下面是到目前爲止的代碼是我的工作,這將是一個評論系統,流量:我的jQuery Ajax表單不會發表

  1. 用戶填寫意見表
  2. 點擊提交, AJAX調用時和 張貼到PHP腳本

  3. 腳本可確定用戶應 輸入驗證碼形式

  4. 如果他們需要一個CA ptcha形成它發送 與有 消毒消息

  5. 一起迴響應。如果他們不需要驗證碼,它 進行消毒的消息,併成功響應

  6. 將其發送 回來,如果有錯誤它發回一個 錯誤消息。

  7. 如果成功,我們也該消息 添加到頁面

好自己的問題,到目前爲止是,當我點擊提交,重新加載的,而不是做Ajax調用的頁面,你看任何明顯的原因?

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#load').hide(); 
    //load facebox 
    $('a[rel*=facebox]').facebox() 
}); 


// add comments 
var messageArea = $('textarea#message'); 
$('input#submit-comment').click(function() { 
    // Store vars 
    var message = messageArea.hide().val(); 
    var dataString = 'message=' + message + '&u=1'; 

    //send comment to php 
    $.ajax({ 
     type: "POST", 
     url: "process.php", 
     data: dataString, 
     dataType: "json", 
     success: function (data) { 
      if (json.response == 'captcha') { 
       //they are mass posting so we need to give them the captcha form 
       // maybe we can open it in some kind of dialog like facebox 
       // have to figure out how I can pass the comment and user data to the captcha script and then post it 
       jQuery.facebox(function() { 
        jQuery.get('captchabox.php', function (data) { 
         jQuery.facebox('<textarea>' + data + '</textarea>') 
        }) 
       }) 
       alert('captcha'); 
      } else if (json.response == 'error') { 
       // there was some sort of error so we will just show an error message in a DIV 
       // just an alert for testing purposes 
       alert('sorry there was an error'); 
      } else if (json.response == 'success') { 
       alert('sucess'); 
       // success append the sanitized-comment to the page 
       $('#comments').prepend(obj.comment); 
      }; 
     } 
    }) 
    return false; 
}); 
</script> 

<div id="container"> 
    <div id="comments"> 
<!-- comments loop will be here from mysql results --> 
     <div class="comment"> 
      <div class="date"> 
       <span class="day-month"><?php echo $dayMonth; ?></span> 
       <span class="year"><?php echo $year; ?></span> 
      </div> 
      <span class="content"><?php echo stripslashes($message);?> <span class="time"><?php echo $datediff; ?></span></span> 
      <div class="clear"></div> 
     </div> 
<!-- end comment loop--> 
    </div> 
    <div id="submission"> 
     <form name="comment-submission"> 
      <textarea id="message" name="message"></textarea> 
      <span class="limit">140</span> 
      <input type="submit" id="submit-comment" value=" " /> 
     </form> 
     <div class="clear"></div> 
    </div> 
</div> 

UPDATE

我的回報虛假增加了;就在ajax調用下面的點擊功能關閉之前,它在頁面上沒有任何變化,它仍然在點擊頁面上重新加載頁面,請任何幫助

回答

1

在您撥打$.ajax()後添加return false;。這將阻止提交元素的默認操作。

+0

我加了return false;插入下面的代碼中,它不會創建任何錯誤,但不會改變任何內容,它是否在錯誤的位置? – JasonDavis 2009-08-11 15:37:40

2

您需要「返回false」;在你點擊()事件,年底或更改

<input type="submit"...> 

<input type="button"...> 
+1

+1我認爲真正的答案是使用按鈕。 – Marc 2009-08-11 15:42:08

+0

我試過這個,但它不適用於我,我甚至把警報('captcha');只是在點擊事件中,它甚至不會觸發該關閉 – JasonDavis 2009-08-11 15:51:08

+0

然後,您的代碼語法出現問題。檢查螢火蟲或其他JavaScript調試器,看看是否有任何錯誤被拋出 – Mark 2009-08-11 16:03:07

0

您將需要從按鈕的點擊處理程序返回false,以阻止該網頁發佈。