你可以这样做:
// Hide lis
$(".training_screen").hide();
$(".welcome_screen").hide();
// Show/Hide forms based on radio button selection
$('input[name="screen_type"]').bind('change',function(){
var fnTraining, fnWelcome;
if ($(this).val() == 1) {
fnTraining = "slideDown";
fnWelcome = "slideUp";
} else {
fnTraining = "slideUp";
fnWelcome = "slideDown";
}
$('.welcome_screen')[fnWelcome]();
$('.training_screen')[fnTraining]();
});
工作演示:http: //jsfiddle.net/jfriend00/4aSbG/
或者,您可以创建一个带有 show/hide 参数的 slideToggleOption jQuery 方法,并像这样使用它:
// Hide lis
$(".training_screen").hide();
$(".welcome_screen").hide();
jQuery.fn.slideToggleOption = function(show /* other optional args from slideToggle */) {
var args = Array.prototype.slice.call(arguments, 1);
if (show) {
return(this.slideDown.apply(this, args));
} else {
return(this.slideUp.apply(this, args));
}
};
// Show/Hide forms based on radio button selection
$('input[name="screen_type"]').bind('change',function(){
var showWelcome = ($(this).val() == 1);
$('.welcome_screen').slideToggleOption(showWelcome);
$('.training_screen').slideToggleOption(!showWelcome);
});
工作演示:http: //jsfiddle.net/jfriend00/Lqeqe/