如何减少这个脚本

How to reduce this script?

本文关键字:脚本 何减少      更新时间:2023-09-26

我想在这个脚本中使用循环,但我不知道如何做到这一点。

我试过了:

$('#choice').change(function(){
        if ($('#choice').val()=='')
        {
             $('#topic1').hide();
             $('#topic2').hide();
             $('#topic3').hide();
             $('#topic4').hide();
             $('#topic5').hide();
             $('#topic6').hide();
             $('#topic7').hide();
        }if ($('#choice').val()=='1')
        {
             $('#topic1').show();
             $('#topic2').hide();
             $('#topic3').hide();
             $('#topic4').hide();
             $('#topic5').hide();
             $('#topic6').hide();
             $('#topic7').hide();
        }
        if ($('#choice').val()=='2')
        {
             $('#topic1').show();
             $('#topic2').show();
             $('#topic3').hide();
             $('#topic4').hide();
             $('#topic5').hide();
             $('#topic6').hide();
             $('#topic7').hide();
        }
        if ($('#choice').val()=='3')
        {
             $('#topic1').show();
             $('#topic2').show();
             $('#topic3').show();
             $('#topic4').hide();
             $('#topic5').hide();
             $('#topic6').hide();
             $('#topic7').hide();
        }
        if ($('#choice').val()=='4')
        {
             $('#topic1').show();
             $('#topic2').show();
             $('#topic3').show();
             $('#topic4').show();
             $('#topic5').hide();
             $('#topic6').hide();
             $('#topic7').hide();
        }
        if ($('#choice').val()=='5')
        {
             $('#topic1').show();
             $('#topic2').show();
             $('#topic3').show();
             $('#topic4').show();
             $('#topic5').show();
             $('#topic6').hide();
             $('#topic7').hide();
        }
        if ($('#choice').val()=='6')
        {
             $('#topic1').show();
             $('#topic2').show();
             $('#topic3').show();
             $('#topic4').show();
             $('#topic5').show();
             $('#topic6').show();
             $('#topic7').hide();
        }
          if ($('#choice').val()=='7')
        {
             $('#topic1').show();
             $('#topic2').show();
             $('#topic3').show();
             $('#topic4').show();
             $('#topic5').show();
             $('#topic6').show();
             $('#topic7').show();
        }                        
    });
    $('#choice').change();
    });        

$('#choice').change(function(){
        $('[id^="topic"]').hide();
        var topic = $('#choice').val();
        if (topic!='') {
            $('#topic'+topic).show();
        };
});        
$('#choice').change();

试试这个

$('#choice').change(function(){
var i;
for(i=1;i<8;i++)
{
  if(i==$(this).val())
  {
       $('#topic'+i).show();
  }
  else
  {
       $('#topic'+i).hide();
  }
}
});
$('#choice').change();

编辑:改进了MrCode的建议。

jquery可以使用如下选择器:

$('#choice' + i)

那么,你可以在循环中使用"i"变量

试试这个:

$('#choice').change(function(){
        for( var i = 0; i<8; i++) {
            if($('#choice').val() <= i) { 
                $('#topic' + i).show();
            } else {
                $('#topic' + i).hide();
            }
        }
    }

看起来可以这样做:

$('#choice').on('change', function(){
  for(var i=1;i<8;i+=1)  {
    var showhide = i <= +$(this).val() ? 'show' : 'hide';
    //                  ^convert value to numeric
    $('#topic'+i)[showhide]();
  }
 });

处理程序根据#choice的值(显示或隐藏)确定要使用的方法。对于所有i(在循环内)小于#choice#topic[i],方法是show,对于其他#topic[i],方法是hide。接下来,使用括号符号执行确定的方法。