(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");
// }
}
}