我将选择标记动态添加到表中,如下所示:
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);
我需要检查用户是否选择了所有选项。 (即在我提交之前,有3行应该可以选择所有值),如下所示: (在提交之前,我需要检查1名初级审计员,1名二级审计员和1名Audtiee)
+-------------+--------------+------------------------------------------+
| Username + Required | Level |
+-------------+--------------+------------------------------------------+
| User 1 | YES | Dropdown (Primary Auditor Selected) |
+-------------+--------------+------------------------------------------+
| User 2 | YES | Dropdown (Secondary Auditor Selected) |
+-------------+--------------+------------------------------------------+
| User 3 | YES | Dropdown (Auditee Selected) |
+-------------+--------------+------------------------------------------+
我尝试了以下内容:
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;
不工作,请帮助我。
答案 0 :(得分:3)
首先,从下拉列表中收集所有选定的值:
var values = $('#tblSeelctedProjectResource select').map(function() {
return this.value;
}).get();
然后检查是否选择了这三个选项(按任意顺序):
return $.inArray("Primary Auditor", values) &&
$.inArray("Secondary Auditor", values) &&
$.inArray("Auditee", values);
答案 1 :(得分:0)
如果我理解了这个问题,你可以在n行中创建一个选择,并且需要检查是否首先选择了“Primary Auditor”值,依此类推。 因此,您需要迭代所有选择并查询它的位置及其值,请尝试此代码
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