如何在MVC上的两个列表框之间移动项目-JQuery不起作用
How do you move items betwen two listboxes on MVC - JQuery not working
我已经研究过其他关于这方面的SO主题,它们最终要么非常旧,要么使用WebForms。我有一个MVC视图,其中有两个列表框。我想在两个列表框之间来回移动项目。视图为:
@using (Html.BeginForm())
{
@Html.ListBoxFor(m => m.SelectedAttributes, Model.Attributes, new {id="listBoxAvail", SIZE = 5} )
<input type="submit" name="add"
id="add" value="MoveRight" />
<input type="submit" name="remove"
id="remove" value="MoveLeft" />
@Html.ListBoxFor(m => m.SelectedAttributes2, Model.SelectedItems, new { id = "listBoxSel", SIZE = 5})
}
ViewModel是:
public class OptInViewModel
{
public IEnumerable<string> SelectedAttributes { get; set; }
public IEnumerable<string> SelectedAttributes2 { get; set; }
public IEnumerable<SelectListItem> Attributes { get; set; }
public IEnumerable<SelectListItem> SelectedItems { get; set; }
}
And the Controller code is:
public ActionResult Index()
{
AttributeEntities db = new AttributeEntities();
List<SelectListItem> listSelectListItems = new List<SelectListItem>();
List<SelectListItem> listSelItems = new List<SelectListItem>();
foreach (var attributes in db.HarmonyAttributes)
{
SelectListItem selectList = new SelectListItem
{
Text = attributes.AttributeName,
Value = attributes.AtrributeLabel,
Selected = false
};
listSelectListItems.Add(selectList);
}
foreach (var sel in db.SelectedHarmonyAttributes)
{
SelectListItem selList = new SelectListItem
{
Text = sel.CustomLabel,
Value = sel.HarmonyAttribute_ID.ToString(),
Selected = false
};
listSelectListItems.Add(selList);
}
OptInViewModel viewModel = new OptInViewModel
{
Attributes = listSelectListItems,
SelectedItems = listSelItems
};
return View(viewModel);
}
我使用JQuery尝试这样做,但它不起作用(没有任何东西转移到第二个列表框)。有人知道怎么了吗?
<script src="~/Scripts/jquery-2.1.1.js"></script>
<script>
$(function () {
$("add").click(function () {
$("#listBoxAvail > option:selected").each(function () {
$(this).remove().appendTo("#listBoxSel");
});
});
$("remove").click(function () {
$("#listBoxSel > option:selected").each(function () {
$(this).remove().appendTo("#listBoxAvail");
});
});
});
</script>
将按钮类型从submit to替换为下面的按钮
<input type="button" name="add" id="add" value="MoveRight" />
<input type="button" name="remove" id="remove" value="MoveLeft" />
在您的JavaScript正确选择器中,将#前置到id,它应该可以工作!
$("add")
至$("#add")
和
$("remove")
至$("#remove")
如果你想,你可以把它减少到
<script>
$(function() {
$(document)
.on("click", "#add", function() {
$("#listBoxAvail :selected").remove().appendTo("#listBoxSel");
})
.on("click","#remove", function() {
$("#listBoxSel :selected").remove().appendTo("#listBoxAvail");
});
});
</script>
您可以将jQuery-Dual-Listbox
插件用于jQuery。它需要一些设置,但在MVC环境中运行良好。需要注意的一点是,为了将第二个列表框中选定的值发布回服务器进行处理,您需要确保在提交表单之前,该列表框中的所有项目都是selected
。
例如,对于您的标记:
<script src="jQuery.dualListBox-1.3.js" type="text/javascript"/>
<script type="text/javascript">
$(function () {
$.configureBoxes({
box1View: 'listBoxAvail',
box2View: 'listBoxSel',
to1: 'remove',
to2: 'add',
allTo1: 'remove-all', //you must create this button
allTo2: 'add-all', //you must create this button
useFilters: false
});
});
$("#listBoxSel").closest("form").on("submit",
function() {
//only selected options in listbox get POSTed back!
$("#listBoxSel > option").prop("selected", true);
return true;
}
}
</script>
源
相关文章:
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 根据两个下拉列表的默认值禁用按钮
- 根据前两个下拉ID显示第三个下拉列表
- 修复列表的绝对位置(li's隐藏在前两个后面)
- 使用两个下拉列表的值填充文本框
- 如何在Javascript中交换两个列表的子列表
- 需要在 html 中使用两个下拉列表来显示不同的 json 值
- 两个下拉列表中的文本字段
- Meteor:如何使用空格键创建包含两个集合数据的树列表
- 使用Jquery比较Javascript中的两个列表/表
- 如何在AngularJS中显示基于两个下拉列表的内容
- jquery-mobile的列表视图中的两个弹出窗口
- 将同一个元素追加到两个下拉列表中只会导致最后一个元素被追加
- jQuery验证插件-比较两个选择下拉列表的自定义方法
- 如何使两个角度列表在html表中并排显示
- 如何在同一个网页上创建两个连接列表
- Kendo Grid UI水印列或一列的两个下拉列表
- AngularJS:用两个JSON数组填充复选框列表,最初检查了一个数组中的一些项
- jQuery ui自动完成下拉列表中的两个字段
- 选择绑定到同一范围的按钮,单击时会相互触发.如何从angular.js中的两个选择列表按钮解除范围绑定