我对Ajax和JavaScript还不熟悉,所以我很抱歉错过了一些可能是非常基本的东西。我正试着提交一份模糊的表格,我正确地调用了函数,但是不知道如何引用表单中的变量。当我从表单中移动时,我在Firebug中看到它正在调用addNotes.php文件,但是没有传递变量。
JavaScript代码:
// Ajax to submit an edited post to the database.
function sendNotes() {
// we want to store the values from the form input box, then send via ajax below
var $form = $(this);
$.ajax({
type: "POST",
url: "includes/addNotes.php",
data: $form.serialize(),
success: function(){
}
}); // End .ajax function
return false;
} //End submit function()
HTML代码:
<form id="adminNotes" method="post" name="adminNotes" action="">
<textarea name="notes" id="notes" onblur="sendNotes()"></textarea>
</form>
Figured it out. Needed to set
var $form = $("form#adminNotes);
I haven't used .serialize
, but try selecting the <form>
instead of the <textarea>
as you are currently doing.
Use $("#adminNotes")
instead of $(this)
in your sendNotes function.