如何从所选元素的属性值中获取对象

时间:2019-01-24 15:48:56

标签: javascript node.js

我正在通过JavaScript生成列表,并尝试从单击的列表项中获取对象。

 options=[
 {
label: "Contact",
message: "Contact Not Found",
"tab": "contact",
"subTabTitle" : "ContactName",
"subTabId" : "contactTab",
"tabContentId" : "contactData",
"svc" : `${config.getContactSvc()}`
}

,
{
"label": "Account",
"message": "Account Not Found",
"tab": "account",
"subTabTitle" : "AccountNumber",
"subTabId" : "accountTab",
"tabContentId" : "accountData",
"svc" :`${config.getAccountSvc()}`

},

{
"label": "Asset",
"message": "Asset Not Found",
"tab": "assets",
"subTabTitle" : "SecurityTypeDescription",
"subTabId" : "assetTab",
"tabContentId" : "assetData",
"svc" : `${config.getAccountAssetSvc()}`


 },
 ]

 var $dropdownSearch = $("#dropdownMenu")
for (var obj of options) {
var $menuItem = $(`<li><a class="newanchor" href="#" id= ${obj.label} 
 type="submit">Search ${obj.tab}</a></li>`)
$menuItem.click(() => {     
  //todo
   var promise= search(obj)

})
$dropdownSearch.append($menuItem)
 }

当我单击列表中的任何元素时,我总是最终得到最后一个元素对象。如何获取选定的列表元素对象

1 个答案:

答案 0 :(得分:0)

尝试使用let代替var。

let $dropdownSearch = $("#dropdownMenu")
for (let obj of options) {
let $menuItem = $(`<li><a class="newanchor" href="#" id= ${obj.label} 
 type="submit">Search ${obj.tab}</a></li>`)
$menuItem.click(() => {     
  //todo
   let promise= search(obj)

})
$dropdownSearch.append($menuItem)
 }