我有以下HTML
<div class="col-lg-7 col-md-6 col-sm-6 text-left">
<a href="javascript:;" class="multiSelect active" id="lbAssistingStaff" title="Evelyn Lupardus, Jamison Hennigan, Kajal Jain, Milud De Silvaa"><span style="width: 252px;">Evelyn Lupardus, Jamison Hennigan, Kajal Jain, Milud De Silvaa</span></a>
<div class="multiSelectOptions" style="position: absolute; z-index: 99999; visibility: visible; height: 150px; width: 259px; top: 30px; left: 15px;">
<label class="selectAll">
<input type="checkbox" class="selectAll">Select All</label>
<label class="">
<input type="checkbox" name="lbAssistingStaff[]" value="10307">Dovie Segerson</label>
<label class="checked">
<input type="checkbox" name="lbAssistingStaff[]" value="14172">Kajal Jain</label>
<label>
<input type="checkbox" name="lbAssistingStaff[]" value="10354">Lydia Nikocevic</label>
<label class="checked">
<input type="checkbox" name="lbAssistingStaff[]" value="10355">Margo Mondloch</label>
</div>
<div class="multiSelectOptions" style="position: absolute; z-index: 99999; visibility: visible; height: 150px; width: 259px;">
<label class="selectAll">
<input type="checkbox" class="selectAll">Select All</label>
<label>
<input type="checkbox" name="lbAssistingStaff[]" value="10307">Dovie Segerson</label>
<label class="checked">
<input type="checkbox" name="lbAssistingStaff[]" value="14172">Kajal Jain</label>
</div>
<div class="multiSelectOptions" style="position: absolute; z-index: 99999; visibility: visible; width: 259px;">
<label class="selectAll">
<input type="checkbox" class="selectAll">Select All</label>
<label class="checked">
<input type="checkbox" name="lbAssistingStaff[]" value="14172">Kajal Jain</label>
<label>
<input type="checkbox" name="lbAssistingStaff[]" value="10062">Nevada Morison</label>
</div>
<div class="multiSelectOptions" style="position: absolute; z-index: 99999; visibility: visible; height: 150px; width: 412px;">
<label class="selectAll">
<input type="checkbox" class="selectAll">Select All</label>
<label>
<input type="checkbox" name="lbAssistingStaff[]" value="10259">Abel Schear</label>
<label class="checked">
<input type="checkbox" name="lbAssistingStaff[]" value="10405">Adam Sandling</label>
<label>
<input type="checkbox" name="lbAssistingStaff[]" value="10400">Alta Barba</label>
<label class="checked">
<input type="checkbox" name="lbAssistingStaff[]" value="16224">amali silva</label>
</div>
<input type="hidden" name="ctl00$ContentPlaceHolder1$ctlViewFilters$hndAssistingStaff" id="hndAssistingStaff" value="10276,10352,14172,14177,10276,10352,14172,10276,10352,14172,14172,14172,14172,14172,10276,10352,14172,10276,10352,14172">
</div>
是通过JQuery多重选择生成的。存在单独的div,它们具有相同的类名称,称为class="multiSelectOptions"
。现在我只需要在第一个Div(top Div)内部将所有选中的选项值推送到Array中即可。
<div class="multiSelectOptions" style="position: absolute; z-index: 99999; visibility: visible; height: 150px; width: 259px; top: 30px; left: 15px;">
<label class="selectAll">
<input type="checkbox" class="selectAll">Select All</label>
<label class="">
<input type="checkbox" name="lbAssistingStaff[]" value="10307">Dovie Segerson</label>
<label class="checked">
<input type="checkbox" name="lbAssistingStaff[]" value="14172">Kajal Jain</label>
<label>
<input type="checkbox" name="lbAssistingStaff[]" value="10354">Lydia Nikocevic</label>
<label class="checked">
<input type="checkbox" name="lbAssistingStaff[]" value="10355">Margo Mondloch</label>
</div>
我尝试过了,
$("#lbAssistingStaff").next("div").click(function (e) {
var listboxID = "lbAssistingStaff";
var selectedItemIDs = [];
$('input[name="' + listboxID + '\[\]"]:checked').each(function () {
selectedItemIDs.push($(this).val());
});
});
但这遍历了所有div的name="lbAssistingStaff[]"
,根据我的要求,数组应该仅具有这两个值( 14172,10355
)。
答案 0 :(得分:2)
提供上下文。
$('input[name="' + listboxID + '\[\]"]:checked', this)
这会将选择器应用于您单击的div的this
。
答案 1 :(得分:1)
在$(this)
中使用find
:
$("#lbAssistingStaff").next("div").click(function (e) {
var listboxID = "lbAssistingStaff";
var selectedItemIDs = [];
$(this).find('input[name="' + listboxID + '\[\]"]:checked').each(function () {
// ^^^^^^^^^
selectedItemIDs.push($(this).val());
});
});
侧面说明:jQuery提供了一个map
函数,您可以使用它来代替创建数组然后推送到它:
var selectedItemIDs = $(this).find('input[name="' + listboxID + '\[\]"]:checked').map(function () {
return this.value; // No need to wrap it just to get the value
}).get();
请不要忘记最后的.get()
,否则您将获得一个jQuery对象而不是一个真正的数组。