我正在创建一个私人消息系统,以允许用户在网站上下文(即,不是电子邮件)内相互通信。使用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