【发布时间】:2021-03-07 09:06:16
【问题描述】:
我只展示了我的代码的 JS 部分,因为 .html 部分在一个表单中只有一个 name="my-input" 的输入框。 在此功能中发生的情况是,如果我在输入框中输入“冒险”,它会给我明日边缘作为电影的标题,如果我输入浪漫,它也会给我 Lalaland。现在我想要的是,显示在 movieList1() 函数中,例如,我有两个具有相同类型“冒险”的对象,然后我希望同时显示电影标题“明日边缘”和“黑暗力量”我的 ID 为“此处”的空列表。如果类型相似,我希望显示所有电影的标题,但我一次只能获得一个电影标题。我是 JS 新手,对象和对象数组看起来有点混乱。任何帮助将不胜感激。
function movieList(){
//This function is called in an 'onkeyup' event inside the input box.
var x = document.forms["my-form"]["my-input"].value; // x has what the user enters inside the
inputbox.
const objList = [{
title:'Edge of Tommorow',
Genre: 'Adventure',
},
{
title:'DarkForce',
Genre: 'Tactical',
},
{
title:'LalaLand',
Genre:'Romantic'
}];
objList.forEach((ele,index) => {
if(ele.Genre=='Adventure' && x==ele.Genre) {
var ans = ele.title;
document.getElementById('here').innerHTML = ans; // 'here' has the id of an empty <li></li> where
the title is shown.
}
else if(ele.Genre=='Tactical' && x==ele.Genre)
{
var ans = ele.title;
document.getElementById('here').innerHTML = ans;
}
else if(ele.Genre=='Romantic' && x==ele.Genre)
{
var ans = ele.title;
document.getElementById('here').innerHTML = ans;
}
else if(x=='')
{
document.getElementById('here').innerHTML='';
}
});
}
function movieList1(){
//This function is called in an 'onkeyup' event inside the input box.
var x = document.forms["my-form"]["my-input"].value; // x has what the user enters inside the input
box.
const objList = [{
title:'Edge of Tommorow',
Genre: 'Adventure',
},
{
title:'DarkForce',
Genre: 'Tactical, Adventure',
},
{
title:'LalaLand',
Genre:'Romantic,Tactical'
}];
objList.forEach((ele,index) => {
if(ele.Genre=='Adventure' && x==ele.Genre) {
var ans = ele.title;
document.getElementById('here').innerHTML = ans; // 'here' has the id of an empty <li></li> where
the title is shown.
}
else if(ele.Genre=='Tactical' && x==ele.Genre)
{
var ans = ele.title;
document.getElementById('here').innerHTML = ans;
}
else if(ele.Genre=='Romantic' && x==ele.Genre)
{
var ans = ele.title;
document.getElementById('here').innerHTML = ans;
}
else if(x=='')
{
document.getElementById('here').innerHTML='';
}
});
}
【问题讨论】:
-
您能否更清楚地了解输入输出:例如 --> Genre=='Tactical' 然后显示 (DarkForce, LalaLand) 对吗?我专注于函数movieList1。
标签: javascript javascript-objects arrayobject