我正在尝试编写一些更简洁的 Javascript / jQuery 代码。我如何将此功能重构为更清洁和更小的东西。绝对似乎有更好的方法来做到这一点。它有效,但我确信有更好的方法来编写这样的函数。提前致谢!
function filterForm(purpose, entry) {
switch (purpose) {
case 'Business' :
switch (entry) {
case 'Single':
$("#moreq1").css( "display", "block" );
$("#moreq2").css( "display", "none" );
$("#moreq3").css( "display", "none" );
break;
case 'Double':
$("#moreq1").css( "display", "block" );
$("#moreq2").css( "display", "none" );
$("#moreq3").css( "display", "none" );
break;
case 'Multiple' :
$("#moreq1").css( "display", "block" );
$("#moreq2").css( "display", "block" );
$("#moreq3").css( "display", "none" );
break;
}
break;
case 'Private' :
switch (entry) {
case 'Single' :
$("#moreq1").css( "display", "block" );
$("#moreq2").css( "display", "none" );
$("#moreq3").css( "display", "none" );
break;
case 'Double' :
$("#moreq1").css( "display", "none" );
$("#moreq2").css( "display", "none" );
$("#moreq3").css( "display", "none" );
break;
case 'Multiple' :
$("#moreq1").css( "display", "none" );
$("#moreq2").css( "display", "none" );
$("#moreq3").css( "display", "none" );
break;
}
break;
case 'Tourist' :
switch (entry) {
case 'Single' :
$("#moreq1").css( "display", "block" );
$("#moreq2").css( "display", "none" );
$("#moreq3").css( "display", "block" );
break;
case 'Double' :
$("#moreq1").css( "display", "none" );
$("#moreq2").css( "display", "none" );
$("#moreq3").css( "display", "none" );
break;
case 'Multiple' :
$("#moreq1").css( "display", "none" );
$("#moreq2").css( "display", "none" );
$("#moreq3").css( "display", "none" );
break;
}
break;
}
}
}