2010-08-14 70 views
2

美好的一天。jQuery替换开关

我在寻找替换jQuery中的switch。基本上,我不知道如何替换switch

我得到了以下switch,约70个国家。有什么方法可以用loop来代替吗?

$("#country").change(function() { 
    switch ($('#country :selected').val()) { 
    case 'pl': 
    $("fieldset#state").hide().load('pl.txt').fadeIn(800); 
    break; 
    } 
}); 

此外,是否有任何可能性实现自动加载特定的文件,如果有已经选定的项目?

编辑:

我对这个问题的描述是不是最好的。对不起。主要问题是:

  • 我只列出了部分国家,而不是所有的人都
  • 我用switch国别读取文件,如果没有,我被默认
  • 插入文本字段
  • 我也需要实现默认加载文件。我是什么意思?我查询数据库的国家,然后我选择它在国家下拉列表中。如何自动加载文件(如果有)?

问候, 汤姆

回答

5

你可以这样做:

$("#country").change(function() { 
    $("fieldset#state").hide().load($('#country :selected').val() + '.txt').fadeIn(800); 
}); 

编辑
有关可用国家列表,你可以把它们放在一个数组,然后执行搜索

$("#country").change(function() { 
    var supportCountries = ['pl',]; //put more here 
    var country = $('#country :selected').val(); 
    if(supportCountries.indexOf(country)) 
     $("fieldset#state").hide().load(country + '.txt').fadeIn(800); 
    else 
     $("fieldset#state").hide().load('default.txt').fadeIn(800); //here is load the default text, change if you has another way. 
}); 

更多详细信息,如果您想更换switch,再拿for/loop找到匹配的情况下, ,然后执行该案件的行动。

+0

由于'this'指的是'