我正在創建一個私人消息系統,以允許用戶在網站上下文(即,不是電子郵件)內相互通信。使用JQuery/Javascript爲表單動態添加值
我已經創建了這個函數來處理我所有的表單提交。我想在不修改此功能的情況下實現解決方案(理想情況下)。
$("form.reload").submit(function(){
alert($(this).serialize()); /* Debugging */
$.post($(this).attr("action"),
$(this).serialize()
,function(data){
if(data){
$("#error").html(data);
}else{
location.reload();
};
});
return false;
});
在我的表單中,我使用JQuery Autocomplete來查找用戶名。這個功能完美運作。我的第一個解決方案是在表單中添加具有必要值的按鈕。
select: function(event, ui) {
$("#message_to").append("<input type=\"button\" class=\"recipient\" name=\"recipients[]\" value=\"" + ui.item.label + "\" onclick=\"$(this).remove()\" />");
}
<form method="post" action="/messages-create" class="reload">
<div id="message_to"></div>
</form>
由於某些原因收件人的值沒有發佈。
我的第二個解決辦法是添加到已在形式
select: function(event, ui) {
$("input[name=recipients[]").val = ui.item.label; /* Need to add to array, not replace! */
$("#message_to").append("<input type=\"button\" class=\"recipient\" name=\"recipient\" value=\"" + ui.item.label + "\" onclick=\"$(this).remove(); /* Remove from recipients */\" />");
}
<form method="post" action="/messages-create" class="reload">
<input type="hidden" name="recipients[]" value="" />
<div id="message_to"></div>
</form>
該工程確定存在後陣,但我一直無法工作,如何追加到recipients[]
陣列只與更換新標籤。從這裏繼續,我還需要知道如何從數組中刪除這個值。
在此先感謝!
工作完美,謝謝! – bigstylee 2010-04-09 19:01:58
+1。比我的大型企業級腳本解決方案更清潔! – 2010-04-09 20:26:50