【发布时间】:2010-12-15 08:05:04
【问题描述】:
我有两个选择列表,每个显示用户和两个按钮(添加和删除),将用户从左侧移动到右侧选择列表,反之亦然。选择列表中用户的添加和删除是使用 jQuery 1.4.4 完成的。问题是,在我移动 至少 一个用户并单击提交后,$_POST 数组不包含任一选择列表。如果我重新加载页面并单击提交而不移动任何用户,则选择列表将出现在 $_POST 数组中。
我用于添加按钮的代码是:
$("#btnAdd").click(function() {
// check if there are any unassigned users selected
if($("#Unassigned_users").val()) {
// for all selected options ...
$("#Unassigned_users option:selected").each(function(n) {
var newOption = \'<option value="\' + $(this).val() + \'">\' + $(this).text() + \'</option>\';
// check if there are any options in the assigned users
if ($("#Assigned_users option").size()) {
// put the item after the last option
$("#Assigned_users option:last").after(newOption);
}
// the assigned users list is empty
else {
// replace the contents of the select tag with the new option
$("#Assigned_users").html(newOption);
}
// finally, remove the selected item
$(this).remove();
});
}
});
Unassigned_users 和 Assigned_users 是选择元素标识符。
【问题讨论】:
-
您的语法无效,只需查看此处突出显示的颜色(提示:转义引号)。复制+粘贴问题还是真实问题?
-
是的,那是因为我在 PHP 字符串中有这段代码 sn-p。我忘了取消转义内容。
-
不要在另一种语言的字符串中编写一种语言的代码。至少使用 HEREDOC 语法。 :)
-
是的,这是一个不错的建议。谢谢你。 :-)
标签: php jquery forms select tags