Forms 使用AJAX提交表单,并将内容放在jQuery验证程序之后的DIV中
全部,, 我有以下代码:Forms 使用AJAX提交表单,并将内容放在jQuery验证程序之后的DIV中,forms,serialization,jquery,Forms,Serialization,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
$(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中显示结果。它不起作用,但我不知道为什么。任何想法都将不胜感激。提前谢谢。您是否认为。提交将提交表格?所做的只是为提交创建时设置一个处理程序 在回调发生之前,您仍然需要提交表单 另外,我不熟悉“提交表单”的方法。为什么不试着用一个普通的表单提交按钮而不是一个链接呢
实际上,我最终拆分了这些函数,新代码是:
$("#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
}
“它不起作用”是什么意思?警报会弹出吗?@Ivan没错。警报不会弹出,并且没有任何内容进入div。在我的save_review.php文件中,我只是有一个回音,上面写着“它工作”,只是为了看看结果是否会填充div。这也不会发生。@user1048676:好的,那么哪一部分不工作?你试过调试吗?正在发送ajax请求吗?如果是,是否发送到正确的url?你到底有什么问题?@Ivan在这种情况下我如何调试它。我在Ajax函数之前尝试了一个警报,但没有显示,然后我在成功中遇到了另一个警报,并且没有显示它。
else{alert('not validate.');}
如何向您的if($(“#create#u review”).valid()
?