我很難過。我使用jquery和ajax從表單向數據庫發佈一些字段。jquery ajax沒有設置變量爲POST
這是一個「編輯表單」 - 所有的字段都預填充mysql數據庫中存在的數據。我從4個字段傳遞輸入,並且僅適用於其中的2個。這裏的HTML
<form id="editSubmit" method="post" name="editSubmit" action="" enctype="multipart/form-data">
<input type="hidden" id="newsID" name="newsID" value="<?=$newsID;?>">
<input type="hidden" id="authorID" name="authorID" value="<?=$news['editorID'];?>">
<label for="postTitle">Title</label><br />
<input type="text" name="postTitle" id="postTitle" class="text" size="20" value="<?=$postTitle;?>"/><br />
<label for="postText">Post Text</label><br />
<textarea name="postText" id="postText" class="textarea"><?=$postText;?></textarea> <br /><br />
<button type="submit">Submit Edit </button>
<input type="button" onClick="closeEdit()" value="Cancel">
</form>
現在這裏是我用來發布這個頁面的代碼。
$("form#editSubmit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var newsID = $('#newsID').attr('value');
var postTitle = $('#postTitle').attr('value');
var postText = $('#postText').attr('value');
postText = postText.replace(/&/g,'%26');
var authorID = $('#authorID').attr('value');
$.ajax({
type: "POST",
url: "news/editNews.php",
data: "newsID="+ newsID + "&postTitle="+ postTitle + "&postText=" + postText + "&authorID=" + authorID,
success: function(){
$('form#editSubmit').fadeOut('slow');
$('div.success').fadeIn();
}
}); // End .ajax function
return false;
}); //End submit function()
此代碼成功發送通過authorID和newsID,但沒有與postTitle或postText運氣。我看不到我做錯了什麼。也許我錯過了什麼?
另外,我是ajax/jquery的全新手 - 所以如果代碼中有些奇怪的東西,我會很感激任何提示。得到這一點已經有很多嘗試和錯誤。
Ooo,我還沒有聽說過序列化。這工作完美。謝謝! – Andelas 2009-10-22 03:43:23