SRK SRK - 5 days ago 5
jQuery Question

Identify all select options within a table

I'm adding select tags dynamically to a table as follows:

selectedRow = '<tr class="trResource">';
selectedRow += '<td align="center" width="15%"><select><option value="Primary Auditor">Primary Auditor</option><option value="Secondary Auditor">Secondary Auditor</option><option value="Auditee">Auditee</option></select></td>';
selectedRow += '</tr>';
$("#tblSeelctedProjectResource tr:first").after(selectedRow);


I need to check, whether the user has selected all the options. (i.e. Before I submit, 3 rows should available with all values selected) as follows:
(I need to check 1 Primary Auditor, 1 secondary Auditor and 1 Audtiee to be selected before i submit)

+-------------+--------------+------------------------------------------+
| Username + Required | Level |
+-------------+--------------+------------------------------------------+
| User 1 | YES | Dropdown (Primary Auditor Selected) |
+-------------+--------------+------------------------------------------+
| User 2 | YES | Dropdown (Secondary Auditor Selected) |
+-------------+--------------+------------------------------------------+
| User 3 | YES | Dropdown (Auditee Selected) |
+-------------+--------------+------------------------------------------+


I tried the following:

var _pa = $('#tblSeelctedProjectResource').find('select option:selected').text() == "Primary Auditor";
var _sa = $('#tblSeelctedProjectResource').find('select option:selected').text() == "Secondary Auditor";
var _ad = $('#tblSeelctedProjectResource').find('select option:selected').text() == "Auditee";
if (!_pa && !_sa && !_ad) {
return false;
}
return true;


Not Working, Please help me on this.

Answer

First, collect all selected values from the drop downs:

var values = $('#tblSeelctedProjectResource select').map(function() {
    return this.value;
}).get();

Then check whether those three options were selected (in any order):

return $.inArray("Primary Auditor", values) && 
    $.inArray("Secondary Auditor", values) &&
    $.inArray("Auditee", values);
Comments