我有(n)个列表框。我有一个执行函数的按钮,在该函数中,我试图获取第一个列表框的选定项目以及在第一个列表框中选择的任何索引/项目,我将在剩余的列表框中选择相同的选项。所有列表框都有完全相同的列表项。
列表框:
@Html.ListBoxFor(model => model.ServiceTypes, new MultiSelectList(RunLog.Domain.Lists.GlobalList.PartsServiceTypes(), "ID", "Name"), new { style = "width: 200px; height: 80px;", id = "lstbox_@(model.PartID)" })
按钮:
@*<input id="button" type="button" class="art" onclick="dosomething()" name="broadcast" value="+" />*@
JS功能:
function dosomething() {
//The following line returns all the listboxes
var listBoxes = var listBoxes = $('select[multiple]');
//In the following line I am trying to access the items from the first listbox but not sure how to access it, would it be by index. it does not work
var x = $('listBoxes[1] option:selected')
//In the following loop I would iterate through the selected items from the first listbox and select them in the rest of the listboxes
for (var i = 0; i < listBoxes.length; i++) {
var element = listBoxes[i];
// if (element.multiple) {
// alert("im a multilistbox");
// }
}
}