Tom*_*Tom 2 jquery switch-statement
美好的一天.
我正在寻找switchjQuery中的替代品.基本上,我不知道如何更换switch.
我跟随switch了大约70个国家.有没有办法用一个替换它loop?
$("#country").change(function () {
switch ($('#country :selected').val()) {
case 'pl':
$("fieldset#state").hide().load('pl.txt').fadeIn(800);
break;
}
});
Run Code Online (Sandbox Code Playgroud)
此外,如果已经选择了项目,是否有可能自动实现加载特定文件?
编辑:
我对这个问题的描述不是最好的.对不起.主要问题是:
switch国家读取文件,如果没有,我默认插入文本字段问候,汤姆
你可以这样做:
$("#country").change(function () {
$("fieldset#state").hide().load($('#country :selected').val() + '.txt').fadeIn(800);
});
Run Code Online (Sandbox Code Playgroud)
编辑:
对于可用国家/地区的列表,您可以将它们放在一个数组中,然后进行搜索
$("#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.
});
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,如果要替换switch,则让我们使用for/ loop来查找匹配的大小写,然后对该大小写执行操作.