2011-12-31 85 views
-1

所有, 我有下面的代码后:提交表单与AJAX和地点的内容在DIV jQuery的验证

$(document).ready(function() { 
    $("#create_review").validate(); 
    $("#submit_review_link").click(function() { 
     if ($("#create_review").valid()) { 
     $("#create_review").submit(function() { // catch the form's submit event 
      $.ajax({ // create an AJAX call... 
       data: $(this).serialize(), // get the form data 
       type: $(this).attr('method'), // GET or POST 
       url: $(this).attr('action'), // the file to call 
       success: function(response) { // on success.. 
        alert("it is here"); 
        $('#created').html(response); // update the DIV 
       } 
      }); 
      return false; // cancel original event to prevent form submitting 
     }); 
     } 
     return false; 
    }); 
}); 

下面是我实际的HTML表单:

<form action="save_review.php" method="post" name="create_review" id="create_review"> 
    <textarea name="review" id="review" rows="5" class="required"></textarea> 
</form> 
<a class="button-2" href="#" id="submit_review_link" onClick="submit_form()">Submit Review</a> 

我想验证这一点,然后在我创建的div中显示结果。它不工作,但我不知道为什么。任何想法将不胜感激。提前致谢。

+0

你是什么意思“它不工作”?警报弹出吗? – Ivan 2011-12-31 17:11:04

+0

Ctrl-Shift-J是你的朋友。 – ranksrejoined 2011-12-31 17:16:32

+0

@伊万,这是正确的。警报不会弹出,没有任何东西进入div。在我的save_review.php文件中,我只是回声说“它工作”只是为了查看结果是否会填充div。这也没有发生。 – user1048676 2011-12-31 17:20:37

回答

0

其实我结束了分裂这些功能和新代码是:

$("#create_review").validate(); 
     $("#submit_review_link").click(function() { 
      if ($("#create_review").valid()) { 
      submit_form(); 
      } 
      return false; 
     }); 

    function submit_form(){ 
     data_to_send = $("#review_text").val(); 
     recipe_id_to_send = $("#recipe_id").val(); 
     current_reviews = $("#current_reviews").val(); 
      $.ajax({ // create an AJAX call... 
       data: {review_info : data_to_send, recipe_id : recipe_id_to_send}, // get the form data 
       type: "POST", // GET or POST 
       url: "save_review.php", // the file to call 
       success: function(response) { // on success.. 
        update_reviews(); 
        $("#review_text").val(""); 
        $('#review_saved').prepend(response); // update the DIV 
        if(current_reviews==0){ 
         $("#no_reviews").hide(); 
        } 

       } 
      }); 
      return false; // cancel original event to prevent form submitting 
    } 
0

您是否认为.submit会提交表单?所有这些都是在创建提交时设置一个处理程序。

您仍然需要在回调发生之前提交表单。

此外,我不熟悉“submit_form”方法。为什么不只是一个正常的表单提交按钮,而不是链接

http://www.javascript-coder.com/html-form/html-form-submit.phtml

<输入型试试=“提交”> < /输入>