Reputation: 70
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.
Upvotes: 2
Views: 604
Reputation: 5193
If I understood well the question, you create a select within n-rows, and you need to check if first has "Primary Auditor" value selected, and so on. So you need to iterate all selects and query it's position and it's value, try this code
var check = true
$('#tblSeelctedProjectResource').find('select').each(function (i,obj){
if ($(obj).text().trim() == "Primary Auditor"){
check &= true
}else if ($(obj).text().trim() == "Secondary Auditor"){
check &= true
}else if ($(obj).text().trim() == "Auditee"){
check &= true
}else{
check = false
}
});
return check
Upvotes: 0
Reputation: 173562
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);
Upvotes: 3