如何减少这个脚本?

ple*_*mer 0 javascript jquery

我想在这个脚本中使用循环,但我不知道该怎么做.

这是我尝试过:

$('#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();
    });        
Run Code Online (Sandbox Code Playgroud)

请在这里帮助我.

Mus*_*usa 5

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