【发布时间】:2015-11-20 08:37:16
【问题描述】:
我有这个 ajax 请求:
SJA.ajax(dataToSend, //this is internal method, which sends requests to the urls
function (respond) {
var categorySelect = that.$modal.find('.cat-post')[0].selectize; //using selectize.js plugin for selects.
if (respond) {
callback && callback(respond);
for (var i in respond) {
//console.log(respond[i].id);
//console.log(respond[i].name);
categorySelect.addOption({value: respond[i].id, text: respond[i].name}); //adding new options to a select field.
}
}
});
var category: that.$modal.find('.cat-post').val() //this is option value in select field !== 'null' ? $('.cat-post').val() : null, //this variable is responsible for keeping selected data.
然后我比较选定的数据(因为我需要获取一些字符串值才能在表中使用它):
var categoryName = 'all';
switch (category) {
case '0' // compare with respond id:
categoryName = "all" // compare with respond name ;
break;
case '1':
categoryName = "Health";
break;
case '2':
categoryName = "Cars";
break;
}
然后我在我的表中添加新的 td。
$tbody.append(['<tr data-id="' + row.id + '">',
'<td class="restricted-view-hide view-mode2-hidden view-mode21-hidden">' + categoryName + '</td>', '</tr>'].join(''));
但我不想每次都在 switch 中输入新值,我想使用一些东西来动态接收新值。 我尝试过这样的事情:
categoryName = respond[i].id != null ? respond[i].name : "any";
但它不起作用。有任何想法吗?
【问题讨论】:
-
您在开关上定义了类别,并且您在传递它时大小写与值不匹配。
-
@Jai 我已经改变了问题。
-
嗨,Max,在任何人尝试回答之前,您确实需要先澄清一下 - 我试过了,问题太多了,太长了!你到底想达到什么目的?您发布的 if/else 语句与 switch 语句完全不同,在过程中使用不同的值。这是故意的吗?
-
@IStanley 对此感到抱歉。我会尝试编辑我的问题。只需 5 分钟。
标签: javascript jquery ajax if-statement switch-statement