Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过输入值检查数组值?_Javascript_Html_Arrays_Input - Fatal编程技术网

Javascript 如何通过输入值检查数组值?

Javascript 如何通过输入值检查数组值?,javascript,html,arrays,input,Javascript,Html,Arrays,Input,我想编写一个程序,它可以从用户输入的字符串中读取值,并可以验证该字符串是否匹配任何数组。如果匹配,则程序将在HTML标记中返回该字符串 我声明了一个名为cars的数组,并通过它创建了一个循环来检查数组的每个值。每次我得到的字符串不可用 <html> <head> <title>Check Whether it is present or not</title> <meta charset="utf-8"

我想编写一个程序,它可以从用户输入的字符串中读取值,并可以验证该字符串是否匹配任何数组。如果匹配,则程序将在HTML标记中返回该字符串

我声明了一个名为cars的数组,并通过它创建了一个循环来检查数组的每个值。每次我得到的字符串不可用

<html>
    <head>
        <title>Check Whether it is present or not</title>
        <meta charset="utf-8">
        <link rel="stylesheet" href="Design.css" />
    </head>
    <body>        
        <div>
            <input type="text" id="txt">
            <input type="button" value="Check" id="btn">
        </div>
        <div>
            <p id="para">para</p>
        </div>
        <script src="Soul.js"></script>
    </body>
</html>



function check() {

    let intText = document.getElementById("txt").value;
    let cars = ["Lamborghini", "BMW", "Ferrari"];

    for (let i = 0; i < cars.length; i++) {
        if (intText == cars[i]) {
            return document.getElementById("para").innerHTML = cars[i] + " is present";
        }
        else {
            return document.getElementById("para").innerHTML = "Not Available";
        } 
    }
    return cars;
}

btn.addEventListener('click', check, false);
如果用户输入BMW,则必须返回BMW是否存在 同样,兰博基尼必须返回兰博基尼在场 但是 Bugatti必须返回Not Present

只需使用include在数组中查找匹配项即可。不需要for循环

cars.includes(intText)
let intText = document.getElementById("txt").value;
let cars = ["Lamborghini", "BMW", "Ferrari"];
功能检查{ 让intText=document.getElementByIdtxt.value; let cars=[兰博基尼、宝马、法拉利]; ifcars.includesintText{ return document.getElementByIdpara.innerHTML=intText+存在; } 否则{ return document.getElementByIdpara.innerHTML=不可用 } } btn.addEventListener'click',check,false; 检查它是否存在

para


您可以通过以下简单条件实现:

var输入=BMW; var输入2=标致 功能检查车{ var汽车=[宝马、兰博、布加迪]; 如果!!~cars.indexOfcar{ 返回车+存在; } 还车+不存在; } console.logcheckCarinput;
console.logcheckCarinput2 除非需要,否则不要使用循环

    function check() {

        let intText = document.getElementById("txt").value;
        let cars = ["Lamborghini", "BMW", "Ferrari"];
        if(cars.includes(intText)){
        document.getElementById("para").innerHTML =intText + " is present";
        } else {
        document.getElementById("para").innerHTML = "Not Available";
        }   
    }

    btn.addEventListener('click', check, false);
匹配时应断开循环。

功能检查{ document.getElementByIdpara.innerHTML=; 让intText=document.getElementByIdtxt.value; let cars=[兰博基尼、宝马、法拉利]; var标志=cars.indexofintext; 如果存在标志>-1return document.getElementByIdpara.innerHTML=intText+; else return document.getElementByIdpara.innerHTML=不可用; } btn.addEventListener'click',check,false; 检查它是否存在

功能检查{ 让intText=document.getElementByIdtxt.value; let cars=[兰博基尼、宝马、法拉利]; 如果cars.indexofintext!=-1{ return document.getElementByIdpara.innerHTML=intText+存在; }否则{ return document.getElementByIdpara.innerHTML=不可用; } 返回车辆; } btn.addEventListener'click',check,false; 检查它是否存在

在数组值的情况下使用indexOf,而不是使用==条件
        <head>
            <title>Check Whether it is present or not</title>
            <meta charset="utf-8">
            <link rel="stylesheet" href="Design.css" />
        </head>
        <body>        
            <div>
                <input type="text" id="txt">
                <input type="button" value="Check" id="btn">
            </div>
            <div>
                <p id="para">para</p>
            </div>
            <script src="Soul.js"></script>
        </body>
    </html>
 <html>
    <head>
        <title>Check Whether it is present or not</title>
        <meta charset="utf-8">
        <link rel="stylesheet" href="Design.css" />
    </head>
    <body>        
        <div>
            <input type="text" id="txt">
            <input type="button" value="Check" id="btn">
        </div>
        <div>
            <p id="para">para</p>
        </div>
        <script src="Soul.js"></script>
    </body>
</html>

<script>

function check() {

    let intText = document.getElementById("txt").value;
    let btn = document.getElementById("btn");
    let cars = ["Lamborghini", "BMW", "Ferrari"];

    let text = "Not Available";
    for (let i = 0; i < cars.length; i++) {
        let car = cars[i];
        if (intText == car) {
            text = car;
            break;
        }
    }
    document.getElementById("para").innerHTML = text + " is present";
}

btn.addEventListener('click', check, false);

</script>