【发布时间】:2010-10-10 04:30:30
【问题描述】:
我主要基于示例 found here 编写了这个 jsfiddle。这个例子已经超过 2 年了,所以我想知道是否有比 if, elseif, else 语句更简单的方法来检查单选按钮的值。我的价值观将是disabled, both, footer, header,所以只有四个还不错。我只是想学习如何编写更好的 Javascript。
HTML:
<input type="radio" name="rdio" value="1" checked="checked" /> 1
<input type="radio" name="rdio" value="2" /> 2
<input type="radio" name="rdio" value="3" /> 3
<div id="div_1"> Div 1 Content</div>
<div id="div_2"> Div 2 Content</div>
<div id="div_3"> Div 3 Content</div>
Javascript:
$('#div_1,#div_2,#div_3').css('display','none');
$("input[name=rdio]").change(function() {
if ($("input[name=rdio]:checked").val() == '1') {
$('#div_1').show();
$('#div_2,#div_3').hide();
}
else if ($("input[name='rdio']:checked").val() == '2') {
$('#div_2').show();
$('#div_1,#div_3').hide();
}
else {
$('#div_3').show();
$('#div_2,#div_1').hide();
}
});
可玩的 JSFiddle:http://www.jsfiddle.net/bs54j/1/
更新:修改问题
所以我在意识到我实际上会做什么之前发布了这个,所以这里是我正在使用的实际代码的更新小提琴:http://jsfiddle.net/BandonRandon/BsupQ/3/
新建 HTML:
<h2> What to show? </h2>
<input type="radio" name="show_custom_header_footer" value="disabled" checked="checked" /> Disabled
<input type="radio" name="show_custom_header_footer" value="both" /> Both
<input type="radio" checked name="show_custom_header_footer" value="header" /> Header
<input type="radio" name="show_custom_header_footer" value="footer" /> Footer
<div id="header_footer_options">
This is the main content div of all the options
<p id="custom_header">Custom Header:<br/> <textarea rows="2" cols="100" name="custom_header"> Custom Header Code</textarea></p>
<p id="custom_footer">Custom Footer:<br/> <textarea rows="2" cols="100" name="custom_footer">Custom Footer Code</textarea></p></div>
新的 Javascript:
//show hide custom header/footer depending on settings
$('#header_footer_options').hide();
//check status on change
$("input[name='show_custom_header_footer']").change(function() {
if ($("input[name='show_custom_header_footer']:checked").val() == 'disabled') {
$('#header_footer_options').fadeOut(500);
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'both') {
$('#header_footer_options').fadeIn();
$('#custom_header,#custom_footer').fadeIn();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'header') {
$('#header_footer_options').fadeIn();
$('#custom_header').fadeIn();
$('#custom_footer').hide();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'footer') {
$('#header_footer_options').fadeIn();
$('#custom_footer').fadeIn();
$('#custom_header').hide();
}
else {
$('#header_footer_options').hide();
}
});
//check status on page load
if ($("input[name='show_custom_header_footer']:checked").val() == 'disabled') {
$('#header_footer_options').hide();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'both') {
$('#header_footer_options').show();
$('#custom_header,#custom_footer').show();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'header') {
$('#header_footer_options').show();
$('#custom_header').show();
$('#custom_footer').hide();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'footer') {
$('#header_footer_options').show();
$('#custom_footer').show();
$('#custom_header').hide();
}
else {
$('#header_footer_options').hide();
}
可能需要重构的最重要的事情是加载时的显示/隐藏我觉得我在那里有大量额外的废话代码。我不是在寻找讲义,只是关于如何使这项工作更好/更快/更强的建议。
【问题讨论】:
标签: javascript jquery refactoring radio-button