I have the following 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>
which generates through the JQuery multi-select.There are separate divs with the same class name called class="multiSelectOptions"
. Now I need to push all the checked options values to Array in only inside the first Div(top Div).which mean this,
<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>
I tried this,
$("#lbAssistingStaff").next("div").click(function (e) {
var listboxID = "lbAssistingStaff";
var selectedItemIDs = [];
$('input[name="' + listboxID + '\[\]"]:checked').each(function () {
selectedItemIDs.push($(this).val());
});
});
But this iterate through all the div's name="lbAssistingStaff[]"
, According to my requirement array should have these two values ( 14172,10355
) only.
Provide the context.
$('input[name="' + listboxID + '\[\]"]:checked', this)
This will apply the selector against the this
which is the div you clicked.
Use find
within $(this)
:
$("#lbAssistingStaff").next("div").click(function (e) {
var listboxID = "lbAssistingStaff";
var selectedItemIDs = [];
$(this).find('input[name="' + listboxID + '\[\]"]:checked').each(function () {
// ^^^^^^^^^
selectedItemIDs.push($(this).val());
});
});
Side note: jQuery offers a map
function you can use instead of creating the array and then pushing to it:
var selectedItemIDs = $(this).find('input[name="' + listboxID + '\[\]"]:checked').map(function () {
return this.value; // No need to wrap it just to get the value
}).get();
Don't forget the .get()
at the end, or you'll get a jQuery object instead of a true array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.