2014-06-05 37 views
2

我正在使用插件Jquery DropdownListJS使用對象

jQuery的下拉插件提供了一個隱藏事件:

$('.dropdown').on('hide', function(event, dropdownData) { 
}); 

在這個函數中,我使用從ListJS插件(以便關閉下拉時被過濾列表)過濾器選項:

$('.dropdown').on('hide', function(event, dropdownData) { 
    hackerList.filter(function(item) { 
    if (item.values().Produkt == "Cola") { 
     return true; 
    } else { 
     return false; 
} 
    }); 
}); 

此代碼正在運行。

存在的問題是:

我想用一個可變對象的值(在這種情況下「PRODUKT」是一個對象的值):

if (item.values().Produkt == "Cola") { 

我怎樣才能改變「PRODUKT」到一個變量?

當只使用item.values().MyVar它不起作用。

+0

嘗試使用item.values() MyVar],應該可以正常工作。 –

回答

0

訪問一個變量,使用bracket notation

var myVar = "test"; 
console.log(yourObject[myVar]); //equivalent to yourObject.test 

所以你的情況做:

var myVar = "Product"; 
$('.dropdown').on('hide', function(event, dropdownData) { 
    hackerList.filter(function(item) { 
     if (item.values()[myVar] == "Cola") { 
      return true; 
     } else { 
      return false; 
     } 
    }); 
}); 
0

試試這個,

eval ("$('.dropdown').on('hide', function(event, dropdownData) {" 
    + "hackerList.filter(function(item) {" 
     +" if (item.values()." +Produkt+ " == 'Cola') {" 
     +"return true;" 
     +"} else {" 
    +"return false;" 
    +"} }); });"); 
+0

你誤解了這個問題,請重新閱讀。 –

+1

使用if(item.values()[variableName] ==「Cola」)代替 – Gabe

+0

謝謝@Gabe!這是我搜索的答案。 – Biberwerke