我有一长串 if-else 语句。我知道有一种更有效的方法可以做到这一点,但对 javascript 还不够熟悉。有人可以提供一些指导吗?
$('#webform-component-primary-credential--0').change (function() {
if ($('#edit-submitted-primary-credential-0').val() == 35) {
$('#edit-submitted-additional-credentials-0-47').attr('checked', false);
$("#edit-submitted-additional-credentials-0-47").attr("disabled", "disabled");
} else {
$("#edit-submitted-additional-credentials-0-47").removeAttr("disabled");
}
if ($('#edit-submitted-primary-credential-0').val() == 41) {
$('#edit-submitted-additional-credentials-0-53').attr('checked', false);
$("#edit-submitted-additional-credentials-0-53").attr("disabled", "disabled");
} else {
$("#edit-submitted-additional-credentials-0-53").removeAttr("disabled");
}
if ($('#edit-submitted-primary-credential-0').val() == 13) {
$('#edit-submitted-additional-credentials-0-29').attr('checked', false);
$("#edit-submitted-additional-credentials-0-29").attr("disabled", "disabled");
} else {
$("#edit-submitted-additional-credentials-0-29").removeAttr("disabled");
}
});
本系列中还有大约 12 个 if-else 语句,但这给出了总体思路。