2017-03-02 75 views
-1
var teams=[ 
    {key:1, label:"Faizel", type:1}, 
    {key:15, label:"Phumzi", type:1}, 
    {key:2, label:"Julian", type:2}, 
    {key:3, label:"Rydel", type:1}, 
    {key:14, label:"Srdjan", type:2}, 
    {key:5, label:"Doi", type:1}, 
    {key:6, label:"Brandon", type:2}, 
    {key:7, label:"Jose", type:1}, 
    {key:8, label:"Lesley", type:1}, 
    {key:9, label:"Robin", type:1}, 
    {key:10, label:"Bigshow", type:2}, 
    {key:11, label:"Template", type:1}, 
    {key:12, label:"Slab View", type:1}, 
    {key:13, label:"Dulla", type:1}, 
    {key:16, label:"Nelson", type:1}, 
]; 

我想基於關鍵是,在文本字段(例如)用戶的輸入,如果在文本字段用戶輸入14的警報應該說「斯爾詹」如何根據用戶的輸入自動顯示警告

+0

使用'alert'顯示警報,否則它只是一個問題去經歷的陣列尋找匹配。 – Richard

+0

您的輸入和事件處理程序在哪裏? – Jai

+0

['Array.prototype.find()'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) – Andreas

回答

0

可以顯示警報使用Array#find。輸入keyinput並點擊click按鈕。

var teams = [{key:1,label:"Faizel",type:1},{key:15,label:"Phumzi",type:1},{key:2,label:"Julian",type:2},{key:3,label:"Rydel",type:1},{key:14,label:"Srdjan",type:2},{key:5,label:"Doi",type:1},{key:6,label:"Brandon",type:2},{key:7,label:"Jose",type:1},{key:8,label:"Lesley",type:1},{key:9,label:"Robin",type:1},{key:10,label:"Bigshow",type:2},{key:11,label:"Template",type:1},{key:12,label:"Slab View",type:1},{key:13,label:"Dulla",type:1},{key:16,label:"Nelson",type:1}], 
 
    input = document.getElementById('inp'); 
 

 
function func(){ 
 
    var prop = teams.find(v => v.key == inp.value).label; 
 
    console.log(prop); 
 
}
<input id='inp'> 
 
<button onclick='func()'>click</button>

+1

謝謝你讓我的一天 –

0

var teams=[ 
 
    {key:1, label:"Faizel", type:1}, 
 
    {key:15, label:"Phumzi", type:1}, 
 
    {key:2, label:"Julian", type:2}, 
 
    {key:3, label:"Rydel", type:1}, 
 
    {key:14, label:"Srdjan", type:2}, 
 
    {key:5, label:"Doi", type:1}, 
 
    {key:6, label:"Brandon", type:2}, 
 
    {key:7, label:"Jose", type:1}, 
 
    {key:8, label:"Lesley", type:1}, 
 
    {key:9, label:"Robin", type:1}, 
 
    {key:10, label:"Bigshow", type:2}, 
 
    {key:11, label:"Template", type:1}, 
 
    {key:12, label:"Slab View", type:1}, 
 
    {key:13, label:"Dulla", type:1}, 
 
    {key:16, label:"Nelson", type:1}, 
 
]; 
 

 
var key = 14; 
 

 
alert(teams.find(function(item){ 
 
    return item.key === key 
 
}).label);

0

輸入編號,然後按下檢查

var teams=[ 
 
{key:1, label:"Faizel", type:1}, 
 
{key:15, label:"Phumzi", type:1}, 
 
{key:2, label:"Julian", type:2}, 
 
{key:3, label:"Rydel", type:1}, 
 
{key:14, label:"Srdjan", type:2}, 
 
{key:5, label:"Doi", type:1}, 
 
{key:6, label:"Brandon", type:2}, 
 
{key:7, label:"Jose", type:1}, 
 
{key:8, label:"Lesley", type:1}, 
 
{key:9, label:"Robin", type:1}, 
 
{key:10, label:"Bigshow", type:2}, 
 
{key:11, label:"Template", type:1}, 
 
{key:12, label:"Slab View", type:1}, 
 
{key:13, label:"Dulla", type:1}, 
 
{key:16, label:"Nelson", type:1}, 
 
]; 
 
function check(){ 
 
var val=document.getElementById('inputT').value; 
 
if(val!=""){ 
 

 
for(var i=0;i<teams.length;i++){ 
 
\t \t if(teams[i].key==val){ 
 
      alert(teams[i].label) 
 
     } 
 
} 
 
} 
 
}
<input type="text" id="inputT"> 
 
<button onclick="check()"> 
 
check 
 
</button>

0

試試:

var teams = [ 
 
    {key:1, label:"Faizel", type:1}, 
 
    {key:15, label:"Phumzi", type:1}, 
 
    {key:2, label:"Julian", type:2}, 
 
    {key:3, label:"Rydel", type:1}, 
 
    {key:14, label:"Srdjan", type:2}, 
 
    {key:5, label:"Doi", type:1}, 
 
    {key:6, label:"Brandon", type:2}, 
 
    {key:7, label:"Jose", type:1}, 
 
    {key:8, label:"Lesley", type:1}, 
 
    {key:9, label:"Robin", type:1}, 
 
    {key:10, label:"Bigshow", type:2}, 
 
    {key:11, label:"Template", type:1}, 
 
    {key:12, label:"Slab View", type:1}, 
 
    {key:13, label:"Dulla", type:1}, 
 
    {key:16, label:"Nelson", type:1}, 
 
]; 
 

 
document.getElementById('displayAlert').addEventListener("click", function() { 
 
    var inputVal = document.getElementById('team').value; 
 
    var selectedTeam = teams.find(function(value) { 
 
    return value.key === parseInt(inputVal); 
 
    }); 
 
    alert(selectedTeam.label); 
 
});
<input type="text" id="team"> 
 
<button id="displayAlert">Display Alert</button>

-1

我建議使用lodash

有了它,說到容易找到基於屬性的對象:

// get the input with jquery 
var value = $('#myinput').val(); 

var resultObject = _.find(teams, function(obj) { 
    return obj.key === parseInt(value); 
}); 

alert(resultObject.label); 
+0

不,我不建議使用lodash。 –

相關問題