我想在这个脚本中使用循环,但我不知道该怎么做。
这是我尝试过的:
$('#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();
关于javascript - 如何减少这个脚本呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14252469/