[英]JavaScript: Cannot properly re-run for loop function w/ if statements
First of all thanks for your help. 首先感谢您的帮助。
I have a JSFiddle here https://jsfiddle.net/tonegzup/zvf0xgev/ 我在这里有一个JSFiddle https://jsfiddle.net/tonegzup/zvf0xgev/
Problem: When first running the page, you will be able to submit "software1" in the text field and get back 3 results, these 3 results are part of an array of objects. 问题:第一次运行页面时,您将能够在文本字段中提交“ software1”并获取3个结果,这3个结果是对象数组的一部分。 You can then run another submit with "aast" and you will get back 1 result. 然后,您可以使用“ aast”运行另一个提交,您将获得1个结果。 Now if you run a submit for "software1" you will only get back 1 result, instead of the original 3. 现在,如果您为“ software1”运行提交,则只会返回1个结果,而不是原始的3个结果。
Band-aid: It is not in the jdFiddle, but right now I have this setup with a "clear" button that just refreshes the page so the user can successfully run the tool again, but there has to be a way for me to get this done right. 创可贴:它不在jdFiddle中,但是现在我使用“清除”按钮进行此设置,该按钮仅刷新页面,以便用户可以再次成功运行该工具,但是必须有一种方法让我获得这做对了。
Also I have tried using jQuery but my jQuery does not run, when I try to use anything with "$" in my .js it just never does anything and I have included jquery-1.11.3.min.js. 我也尝试过使用jQuery,但是我的jQuery无法运行,当我尝试在.js中使用带有“ $”的任何内容时,它什么也没做,并且包含了jquery-1.11.3.min.js。 So I've been trying to stick with javascript. 因此,我一直在尝试使用javascript。
<body>
<section class="page-header">
<h1 class="project-name">PocTool</h1>
<!--<h2 class="project-tagline">poctool</h2>-->
<input class="button" id="app" type="string" onkeypress="enterKey(event)">
<button type="button" onclick="myFunction();">Submit</button>
<div id="anal">
<p>
<ul id="analy1" style="list-style-type:none">
<img id="glyphu"</img>
<li id="name"></li>
<li id="lastname"></li>
<li id="homenum"></li>
<li id="busnum"></li>
<li id="ex"></li>
<li id="cellnum"></li>
<img id="glyphe"</img>
<li id="email"></li>
<li id="company"></li>
</ul>
<p>
<ul id="analy2" style="list-style-type:none">
<img id="glyphu"</img>
<li id="name2"></li>
<li id="lastname2"></li>
<li id="homenum2"></li>
<li id="busnum2"></li>
<li id="ex2"></li>
<li id="cellnum2"></li>
<img id="glyphe"</img>
<li id="email2"></li>
<li id="company2"></li>
</ul>
<p>
<ul id="analy3" style="list-style-type:none">
<img id="glyphu"</img>
<li id="name3"></li>
<li id="lastname3"></li>
<li id="homenum3"></li>
<li id="busnum3"></li>
<li id="ex3"></li>
<li id="cellnum3"></li>
<img id="glyphe"</img>
<li id="email3"></li>
<li id="company3"></li>
</ul>
<p>
<ul id="analy4" style="list-style-type:none">
<img id="glyphu"</img>
<li id="name4"></li>
<li id="lastname4"></li>
<li id="homenum4"></li>
<li id="busnum4"></li>
<li id="ex4"></li>
<li id="cellnum4"></li>
<img id="glyphe"</img>
<li id="email4"></li>
<li id="company4"></li>
</ul>
<p>
<ul id="analy5" style="list-style-type:none">
<img id="glyphu"</img>
<li id="name5"></li>
<li id="lastname5"></li>
<li id="homenum5"></li>
<li id="busnum5"></li>
<li id="ex5"></li>
<li id="cellnum5"></li>
<img id="glyphe"</img>
<li id="email5"></li>
<li id="company5"></li>
</ul>
</div>
</section>
var arr0 = [
{lou: {
firstname: 'Lou ',
lastname: 'Steve',
name: 'Lou Steve',
homenum: '',
busnum: '782-887-7836',
ex: '52484',
cellnum: '553-552-6908',
email: 'Lou.Steve@123.com',
company: ''
}
}, {
perkeisha: {
firstname: 'Perkaisha',
lastname: 'pants',
name: 'Perkeisha pants',
homenum: '754-366-6908',
busnum: '586-727-7834',
ex: '37569',
cellnum: '754-366-6908',
email: 'Perkeisha.pants@123.com',
company: ''
}
}, {
tom: {
firstname: 'Tom ',
lastname: 'Tiny',
name: 'Tom Tiny',
homenum: '859-823-1155',
busnum: '963-553-7837',
ex: '37837',
cellnum: '547-224-5045',
email: 'Tom.Tiny@123.com',
company: ''
}
}
];
var arr = [{
appl: 'software1',
anal1: arr0[1].perkeisha,
anal2: arr0[2].tom,
anal3: arr0[0].lou
},
{
appl: 'aast',
anal1: arr0[1].perkeisha
},
{
appl: 'accent health',
anal1: arr0[1].perkeisha
},
{
appl: 'accudose',
anal1: arr0[2].tom
}
];
var app, arrLength = arr.length;
function myFunction() {
var x, analystSupport, app;
// Get the value of the input field with id="app"
x = document.getElementById("app").value;
app = x.toLowerCase();
for (var i = 0; i < arrLength; i++){
if (app === arr[i].appl) {
console.log(arr[i].anal1.firstname);
console.log(arr[i].anal1.lastname);
var arr1 = [
{analy1 : arr[i].anal1},
{analy2 : arr[i].anal2},
{analy3 : arr[i].anal3},
{analy4 : arr[i].anal4},
{analy5 : arr[i].anal5}
];
if (arr1[0].analy1 === undefined) {
document.getElementById("analy1").innerHTML = " ";
} else if (arr1[0].analy1 !== undefined) {
document.getElementById("glyphu").src= "glyph/glyphicons-4-user.png";
document.getElementById("name").innerHTML = arr1[0].analy1.name;
document.getElementById("homenum").innerHTML = arr1[0].analy1.homenum;
document.getElementById("busnum").innerHTML = arr1[0].analy1.busnum;
document.getElementById("ex").innerHTML = arr1[0].analy1.ex;
document.getElementById("cellnum").innerHTML = arr1[0].analy1.cellnum;
document.getElementById("glyphe").src= "glyph/glyphicons-11-envelope.png";
document.getElementById("email").innerHTML = arr1[0].analy1.email;
document.getElementById("company").innerHTML = arr1[0].analy1.company;
}
if (arr1[1].analy2 === undefined){
document.getElementById("analy2").innerHTML = " ";
} else if (arr1[1].analy2 !== undefined) {
document.getElementById("glyphu").src= "glyph/glyphicons-4-user.png";
document.getElementById("name2").innerHTML = arr1[1].analy2.name;
document.getElementById("homenum2").innerHTML = arr1[1].analy2.homenum;
document.getElementById("busnum2").innerHTML = arr1[1].analy2.busnum;
document.getElementById("ex2").innerHTML = arr1[1].analy2.ex;
document.getElementById("cellnum2").innerHTML = arr1[1].analy2.cellnum;
document.getElementById("email2").innerHTML = arr1[1].analy2.email;
document.getElementById("company2").innerHTML = arr1[1].analy2.company;
}
if (arr1[2].analy3 === undefined){
document.getElementById("analy3").innerHTML = " ";
} else if (arr1[2].analy3 !== undefined) {
document.getElementById("glyphu").src= "glyph/glyphicons-4-user.png";
document.getElementById("name3").innerHTML = arr1[2].analy3.name;
document.getElementById("homenum3").innerHTML = arr1[2].analy3.homenum;
document.getElementById("busnum3").innerHTML = arr1[2].analy3.busnum;
document.getElementById("ex3").innerHTML = arr1[2].analy3.ex;
document.getElementById("cellnum3").innerHTML = arr1[2].analy3.cellnum;
document.getElementById("email3").innerHTML = arr1[2].analy3.email;
document.getElementById("company3").innerHTML = arr1[2].analy3.company;
}
if (arr1[3].analy4 === undefined){
document.getElementById("analy4").innerHTML = " ";
} else if (arr1[3].analy4 !== undefined) {
document.getElementById("glyphu").src= "glyph/glyphicons-4-user.png";
document.getElementById("name4").innerHTML = arr1[3].analy4.name;
document.getElementById("homenum4").innerHTML = arr1[3].analy4.homenum;
document.getElementById("busnum4").innerHTML = arr1[3].analy4.busnum;
document.getElementById("ex4").innerHTML = arr1[3].analy4.ex;
document.getElementById("cellnum4").innerHTML = arr1[3].analy4.cellnum;
document.getElementById("email4").innerHTML = arr1[3].analy4.email;
document.getElementById("company4").innerHTML = arr1[3].analy4.company;
}
if (arr1[4].analy5 === undefined){
document.getElementById("analy5").innerHTML = " ";
} else if (arr1[4].analy5 !== undefined) {
document.getElementById("glyphu").src= "glyph/glyphicons-4-user.png";
document.getElementById("name5").innerHTML = arr1[4].analy5.name;
document.getElementById("homenum5").innerHTML = arr1[4].analy5.homenum;
document.getElementById("busnum5").innerHTML = arr1[4].analy5.busnum;
document.getElementById("ex5").innerHTML = arr1[4].analy5.ex;
document.getElementById("cellnum5").innerHTML = arr1[4].analy5.cellnum;
document.getElementById("email5").innerHTML = arr1[4].analy5.email;
document.getElementById("company5").innerHTML = arr1[4].analy5.company;
}
}
} //end of iterate through 'arr' array
}
function enterKey(e){
var key=e.keyCode || e.which;
if (key==13){
myFunction();
}
}
I figured this out... 我想通了...
when I was doing this 当我这样做的时候
document.getElementById("analy1").innerHTML = " ";
I was creating the problem. 我在制造问题。 Setting the code to 将代码设置为
document.getElementById("analy1").style.display = "none";
Would fix the problem by ensuring the IDs are still available, but make sure they just are not displayed. 将通过确保ID仍然可用来解决问题,但要确保它们不显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.