我能得到一个不同版本的代码与上周我在这里获得了帮助工作。我学习,所以我决定用一个for循环遍历数组这个时间去重构它。这本身似乎直到我添加一个“其他”,以提醒他们进不正确的数据用户正常工作。
我看到里面被for循环它的时候,它遍历循环的次数,但无论在哪里,我把警报,它只是似乎压倒一切。它会弹出先不管输入什么,那么你有它显示阵列中的数据之前,点击5次。我打开其他方法,对只使用警报未设置。
我试着写一个这样的功能,所以我可以四处移动电话进行测试:
function inValidAlert(){
let cylFindInvalid =
document.getElementById("cylEnter").value.toUpperCase();
if(cylFindInvalid != cylArray.name){
let displayIt = document.getElementById("displayCyl");
displayIt.innerHTML = displayIt.innerHTML + "Enter a valid
cylinder type";
}
}
我把invalidAlert()的函数listCylinders里面,功能等之外......这给了我同样的结果只是写一个警告,但无论如何给了我一些实践...
这似乎是没有道理的一些你,但我学习JavaScript和需要帮助推测这一个:)
该代码显示为不警告声明的作品。因此,一个用户输入与一个阵列中的“name”属性的圆筒型。 LD,RD,GD,等等...如果它是有效的,则在对象的数据(在阵列中)显示在屏幕上。但是,如果一个无效的条目看到,我希望有一个弹出警报显示,说“无效数据”或类似。在弹出的工作,但在错误的时间或采取多次点击,以清除它,如果里面的for循环。如果我使用“破”,那么警报将覆盖整个if语句和必火,无论是输入的内容。
所以,我怎么会去获取该通知以适当火?也许for循环开始与错误的总方针?让我知道如果我需要张贴我的HTML也。我是新的,试图在这里学习的绳索,请宽容我。
function listCylinders() {
let display = document.getElementById("displayCyl");
for (var i = 0; i < cylArray.length; i++) {
let cylCompare = document.getElementById("cylEnter").value.toUpperCase();
if (cylCompare == cylArray[i].name) {
display.innerHTML = display.innerHTML + cylArray[i].name + ":" + "<br>" + cylArray[i].brand +
"<br>" + cylArray[i].cylinder + "<br>" + cylArray[i].pins + "<br>" + cylArray[i].type;
} else {
alert("Enter a valid cylinder type");
}
}
}
//function used to disable the button after it is used once.
const setbutton = document.getElementById('button1');
setbutton.addEventListener('click', disableButton);
function disableButton() {
setbutton.disabled = true;
}
//function that will clear the form as well as the display when clicked.
function clearCyl() {
var cylDisplay = document.getElementById("displayCyl");
document.getElementById("cylForm").reset();
cylDisplay.innerHTML = "";
setbutton.disabled = false;
}
//cylinder type properties shown as individual objects inside of an array.
var cylArray = [{
name: 'LD',
brand: 'Schlage, Falcon',
cylinder: ' Without cylinder',
pins: ' 6 Pin',
type: ' KIL'
},
{
name: 'RD',
brand: 'Schlage-Everest 29 S123 (Standard)',
cylinder: ' With cylinder',
pins: ' 6 Pin',
type: ' FSIC'
},
{
name: 'PD',
brand: 'Schlage-Everest 29 S123 (Standard)',
cylinder: ' With cylinder',
pins: ' 6 Pin',
type: ' KIL'
},
{
name: 'JD',
brand: 'Schlage',
cylinder: ' Without cylinder',
pins: ' 6 Pin',
type: ' FSIC'
},
{
name: 'GD',
brand: 'Schlage-Everest 29 R Family keyways',
cylinder: ' With cylinder',
pins: ' 7 Pin',
type: ' SFIC'
}
];
在你的循环逻辑,它会永远提醒至少5倍。这是因为cylCompare
永远不会改变,所以它永远不会等于阵列您回路的其他5个。名称属性。所以cylCompare == cylArray[i].name
永远只能是真正为你的六次迭代之一。因此,警报显示成一排的5倍。
相反,你会想要做的是,如果用户输入永不阵列中相匹配的单个项目,那么你会希望说这是无效的。你可以假设这样做是无效的第一,那么一旦你找到任何匹配其更改为是有效的:
function listCylinders() {
let display = document.getElementById("displayCyl");
let valid = false; // We assume this isn't valid
// You can move this outside of the loop, and trim in case the user entered spaces
let cylCompare = document.getElementById("cylEnter").value.toUpperCase().trim();
for (var i = 0; i < cylArray.length; i++) {
if (cylCompare == cylArray[i].name) {
valid = true; // We find a match in one of our array items, so it's valid
display.innerHTML = display.innerHTML + cylArray[i].name + ":" + "<br>" + cylArray[i].brand +
"<br>" + cylArray[i].cylinder + "<br>" + cylArray[i].pins + "<br>" + cylArray[i].type;
}
}
if(!valid) alert("Enter a valid cylinder type"); // If we never found a match then we alert
}