需要检查变量的条件并打印警报
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了需要检查变量的条件并打印警报相关的知识,希望对你有一定的参考价值。
我想检查条件是否变量countItems等于变量countMatchInput如果它是相等的那么它应该给出一个警告Hi。变量countItems是一个数组
下面的javascript代码
var countItems = ["C++", "C#", "VB", "Java and Ruby","Physics", "Chemistry", "Mathematics", "Social Science", "History", "Geography", "Rickshaw", "Bike", "Car", "Bus and Train"];
function addCounts() {
var countMatchInput = document.getElementById("countMatchInput").value;
document.getElementById("demo").innerhtml = countMatchInput;
for (var i = 0; i < countItems; i++) {
if(countItems === countMatchInput)
{
alert("Hi");
}
}
};
Html代码如下
<textarea id="countMatchInput"></textarea>
<button onclick="addCounts()">
Add
</button>
<p id="demo">
</p>
答案
你的for
循环有问题:
var countItems = ["C++", "C#", "VB", "Java and Ruby","Physics", "Chemistry", "Mathematics", "Social Science", "History", "Geography", "Rickshaw", "Bike", "Car", "Bus and Train"];
function addCounts() {
var countMatchInput = document.getElementById("countMatchInput").value;
document.getElementById("demo").innerHTML = countMatchInput;
for (var i = 0; i < countItems.length; i++) {
if(countItems[i] === countMatchInput){
alert("Hi");
}
}
};
以上是关于需要检查变量的条件并打印警报的主要内容,如果未能解决你的问题,请参考以下文章