【发布时间】:2010-04-10 02:44:14
【问题描述】:
我正在创建一个私人消息系统,以允许用户在网站范围内相互交流(即,不是电子邮件)。
我创建了这个函数来处理我所有的表单提交。我想在不修改此功能的情况下实现解决方案(理想情况下)。
$("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[] 数组仅替换为新标签。从这里开始,我还需要知道如何从数组中删除这个值。
提前致谢!
【问题讨论】:
标签: javascript jquery html forms