我有几个不同的单选按钮,它们返回种族和性别。该脚本在内部应用程序中运行,因此我不会返回“男孩”、“女孩”或“两者”,而是返回 7707330、7707333 和 7707336。与种族单选按钮类似。
然后,我需要根据种族和性别的组合来验证数据。这是一个非常简单的任务,但我最终得到了 15 个 if 语句!一切正常,但必须有更清洁的解决方案?
function test(radioResults) {
var1 = radioResults[0].toString();
var2 = radioResults[1].toString();
var roll = parseFloat(parent.roll);
if (var2 == '7707330') {
gender = 'boy';
}
if (var2 == '7707333') {
gender = 'girl';
}
if (var2 == '7707336') {
gender = 'both';
}
if (var1 == '7707341') {
maori(gender);
}
if (var1 == '7707344') {
pasifika(gender);
}
if (var1 == '7707347') {
all(gender);
}
}
function maori(gender) {
//Maori
if (gender == 'boy') {
ethnicity = parseFloat(parent.getMBoys);
validation(ethnicity);
}
if (gender == 'girl') {
ethnicity = parseFloat(parent.getMGirls);
validation(ethnicity);
}
if (gender == 'both') {
ethnicity = parseFloat(parent.getTotalM);
validation(ethnicity);
}
}
function pasifika(gender) {
//Pasifika
if (gender == 'boy') {
ethnicity = parseFloat(parent.getPBoys);
validation(ethnicity);
}
if (gender == 'girl') {
ethnicity = parseFloat(parent.getPGirls);
validation(ethnicity);
}
if (gender == 'both') {
ethnicity = parseFloat(parent.getTotalP);
validation(ethnicity);
}
}
function all(gender) {
//All
if (gender == 'boy') {
ethnicity = parseFloat(parent.getBoys);
validation(ethnicity);
}
if (gender == 'girl') {
ethnicity = parseFloat(parent.getGirls);
validation(ethnicity);
}
if (gender == 'both') {
ethnicity = parseFloat(parent.getTotalRoll);
validation(ethnicity);
}
}
function validation(ethnicity) {
percent = ethnicity * 5 / 100;
if (ethnicity - percent > roll || (ethnicity + percent < roll)) {
parent.document.getElementById('CF_7784443').value = "FAIL";
} else {
parent.document.getElementById('CF_7784443').value = "PASS";
}
}
我该如何清理这个?