我试图更多地了解 jQuery 以及它是如何工作的。 在我的上一个问题中,用户 Zachary Kniebel 帮助我弄清楚如何根据“RADIO”表单显示/隐藏一些值,现在我修改了他的代码,我想使用循环来改进它(也许是他建议的 FOR 循环大部头书)。实际上我没有运气所以这是我的问题:
**
我忘了说我不能更改 HTML,因为它是由 wordpress 选项框架自动生成的......
**
如何在不更改 HTML 部分的情况下为此功能使用 LOOP?
对于 jsfiddle 爱好者来说,这是一个非常长的 js 代码的工作示例
JS
--
jQuery(document).ready(function ($) {
$types = $('.of-radio');
$types.change(function () {
$this = $(this).val();
if ($this == "0") {
/* zone 1 */
$('#section-custom_posts_name_s_n1').hide();
$('#section-custom_posts_name_p_n1').hide();
$('#section-custom_posts_name_i_n1').hide();
$('#section-custom_posts_name_d_n1').hide();
/* end zone 1 start zone 2*/
$('#section-custom_posts_name_s_n2').hide();
$('#section-custom_posts_name_p_n2').hide();
$('#section-custom_posts_name_i_n2').hide();
$('#section-custom_posts_name_d_n2').hide();
/* end zone 2 start zone 3 */
$('#section-custom_posts_name_s_n3').hide();
$('#section-custom_posts_name_p_n3').hide();
$('#section-custom_posts_name_i_n3').hide();
$('#section-custom_posts_name_d_n3').hide();
/* end zone 3 start zone 4 */
$('#section-custom_posts_name_s_n4').hide();
$('#section-custom_posts_name_p_n4').hide();
$('#section-custom_posts_name_i_n4').hide();
$('#section-custom_posts_name_d_n4').hide();
/* end zone 4 start zone 5 */
$('#section-custom_posts_name_s_n5').hide();
$('#section-custom_posts_name_p_n5').hide();
$('#section-custom_posts_name_i_n5').hide();
$('#section-custom_posts_name_d_n5').hide();
/* end zone 5 */
} else if ($this == "1") {
/* start zone 1 */
$('#section-custom_posts_name_s_n1').show();
$('#section-custom_posts_name_p_n1').show();
$('#section-custom_posts_name_i_n1').show();
$('#section-custom_posts_name_d_n1').show();
/* end zone 1 start zone 2 */
$('#section-custom_posts_name_s_n2').hide();
$('#section-custom_posts_name_p_n2').hide();
$('#section-custom_posts_name_i_n2').hide();
$('#section-custom_posts_name_d_n2').hide();
/* end zone 2 start zone 3 */
$('#section-custom_posts_name_s_n3').hide();
$('#section-custom_posts_name_p_n3').hide();
$('#section-custom_posts_name_i_n3').hide();
$('#section-custom_posts_name_d_n3').hide();
/* end zone 3 start zone 4 */
$('#section-custom_posts_name_s_n4').hide();
$('#section-custom_posts_name_p_n4').hide();
$('#section-custom_posts_name_i_n4').hide();
$('#section-custom_posts_name_d_n4').hide();
/* end zone 4 start zone 5 */
$('#section-custom_posts_name_s_n5').hide();
$('#section-custom_posts_name_p_n5').hide();
$('#section-custom_posts_name_i_n5').hide();
$('#section-custom_posts_name_d_n5').hide();
/* end zone 5 */
} else if ($this == "2") {
/* start zone 1 */
$('#section-custom_posts_name_s_n1').show();
$('#section-custom_posts_name_p_n1').show();
$('#section-custom_posts_name_i_n1').show();
$('#section-custom_posts_name_d_n1').show();
/* end zone 1 start zone 2 */
$('#section-custom_posts_name_s_n2').show();
$('#section-custom_posts_name_p_n2').show();
$('#section-custom_posts_name_i_n2').show();
$('#section-custom_posts_name_d_n2').show();
/* end zone 2 start zone 3 */
$('#section-custom_posts_name_s_n3').hide();
$('#section-custom_posts_name_p_n3').hide();
$('#section-custom_posts_name_i_n3').hide();
$('#section-custom_posts_name_d_n3').hide();
/* end zone 3 start zone 4 */
$('#section-custom_posts_name_s_n4').hide();
$('#section-custom_posts_name_p_n4').hide();
$('#section-custom_posts_name_i_n4').hide();
$('#section-custom_posts_name_d_n4').hide();
/* end zone 4 start zone 5 */
$('#section-custom_posts_name_s_n5').hide();
$('#section-custom_posts_name_p_n5').hide();
$('#section-custom_posts_name_i_n5').hide();
$('#section-custom_posts_name_d_n4').hide();
/* end zone 5 */
} else if ($this == "3") {
/*start zone 1 */
$('#section-custom_posts_name_s_n1').show();
$('#section-custom_posts_name_p_n1').show();
$('#section-custom_posts_name_i_n1').show();
$('#section-custom_posts_name_d_n1').show();
/* end zone 1 start zone 2 */
$('#section-custom_posts_name_s_n2').show();
$('#section-custom_posts_name_p_n2').show();
$('#section-custom_posts_name_i_n2').show();
$('#section-custom_posts_name_d_n2').show();
/* end zone 2 start zone 3 */
$('#section-custom_posts_name_s_n3').show();
$('#section-custom_posts_name_p_n3').show();
$('#section-custom_posts_name_i_n3').show();
$('#section-custom_posts_name_d_n3').show();
/* end zone 3 start zone 4 */
$('#section-custom_posts_name_s_n4').hide();
$('#section-custom_posts_name_p_n4').hide();
$('#section-custom_posts_name_i_n4').hide();
$('#section-custom_posts_name_d_n4').hide();
/* end zone 4 start zone 5 */
$('#section-custom_posts_name_s_n5').hide();
$('#section-custom_posts_name_p_n5').hide();
$('#section-custom_posts_name_i_n5').hide();
$('#section-custom_posts_name_d_n5').hide();
/* end zone 5 */
} else if ($this == "4") {
/* start zone 1 */
$('#section-custom_posts_name_s_n1').show();
$('#section-custom_posts_name_p_n1').show();
$('#section-custom_posts_name_i_n1').show();
$('#section-custom_posts_name_d_n1').show();
/* end zone 1 start zone 2 */
$('#section-custom_posts_name_s_n2').show();
$('#section-custom_posts_name_p_n2').show();
$('#section-custom_posts_name_i_n2').show();
$('#section-custom_posts_name_d_n2').show();
/* end zone 2 start zone 3 */
$('#section-custom_posts_name_s_n3').show();
$('#section-custom_posts_name_p_n3').show();
$('#section-custom_posts_name_i_n3').show();
$('#section-custom_posts_name_d_n3').show();
/* end zone 3 start zone 4 */
$('#section-custom_posts_name_s_n4').show();
$('#section-custom_posts_name_p_n4').show();
$('#section-custom_posts_name_i_n4').show();
$('#section-custom_posts_name_d_n4').show();
/* end zone 4 start zone 5 */
$('#section-custom_posts_name_s_n5').hide();
$('#section-custom_posts_name_p_n5').hide();
$('#section-custom_posts_name_i_n5').hide();
$('#section-custom_posts_name_d_n5').hide();
/* end zone 5 */
} else if ($this == "5") {
/* start zone 1 */
$('#section-custom_posts_name_s_n1').show();
$('#section-custom_posts_name_p_n1').show();
$('#section-custom_posts_name_i_n1').show();
$('#section-custom_posts_name_d_n1').show();
/* end zone 1 start zone 2 */
$('#section-custom_posts_name_s_n2').show();
$('#section-custom_posts_name_p_n2').show();
$('#section-custom_posts_name_i_n2').show();
$('#section-custom_posts_name_d_n2').show();
/* end zone 2 start zone 3 */
$('#section-custom_posts_name_s_n3').show();
$('#section-custom_posts_name_p_n3').show();
$('#section-custom_posts_name_i_n3').show();
$('#section-custom_posts_name_d_n3').show();
/* end zone 3 start zone 4 */
$('#section-custom_posts_name_s_n4').show();
$('#section-custom_posts_name_p_n4').show();
$('#section-custom_posts_name_i_n4').show();
$('#section-custom_posts_name_d_n4').show();
/* end zone 4 start zone 5 */
$('#section-custom_posts_name_s_n5').show();
$('#section-custom_posts_name_p_n5').show();
$('#section-custom_posts_name_i_n5').show();
$('#section-custom_posts_name_d_n5').show();
/* end zone 5 */
}
});
});