Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_If Statement - Fatal编程技术网

Javascript 是否可以从输入中搜索元素数组?

Javascript 是否可以从输入中搜索元素数组?,javascript,arrays,if-statement,Javascript,Arrays,If Statement,我正在创建一个小脚本,尝试根据输入在数组中搜索元素 var modulo=document.getElementById(“modulo”).value; 变量链接=[ "http://www.forumfree.it/", "http://www.forumcommunity.net/", "http://www.blogfree.net/", ]; if(链接的模指数)>-1){ 警报(“您的站点为:”+模); } 否则{ 警报(“对不起,我找不到:”+模) } 数组中的搜索元素 您有


我正在创建一个小脚本,尝试根据输入在数组中搜索元素

var modulo=document.getElementById(“modulo”).value;
变量链接=[
"http://www.forumfree.it/",
"http://www.forumcommunity.net/",
"http://www.blogfree.net/",
];
if(链接的模指数)>-1){
警报(“您的站点为:”+模);
} 
否则{
警报(“对不起,我找不到:”+模)
}

数组中的搜索元素
您有

if(modulo.indexOf(link) > -1)
但我认为应该是这样

if(link.indexOf(modulo) > -1) 
假设模是单个字符串元素,这将在链接数组中搜索该元素并返回该元素的索引。

您已经找到了

if(modulo.indexOf(link) > -1)
但我认为应该是这样

if(link.indexOf(modulo) > -1) 

假设模是单个字符串元素,这将在链接数组中搜索该元素并返回该元素的索引。

您可以尝试使用它使用的事件触发器,使用按钮搜索网站:

<button onclick="myFunction()">Search</button>
搜索
我把你的代码写进了一个函数

我希望这对你有帮助

最后一个国家:

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>

<body>
  <input type="text" id="modulo" class="form">
  <button onclick="myFunction()">Search</button>
</body>

搜寻

var-link=[
"http://www.forumfree.it/",
"http://www.forumcommunity.net/",
"http://www.blogfree.net/",
];
函数myFunction(){
var输入=document.getElementById(“模”);
var结果=[];
对于(变量i=0;i-1){
结果:推送(链接[i]);
}
}
如果(results.length==1){
警报(“您的站点为:“+results[0]);
} 
否则如果(results.length>1){
警报(“抱歉,您的搜索返回多个结果:“+结果”)
}
否则{
警报(“对不起,我找不到:”+input.value)
}

}

您可以尝试使用这个plunker,它使用事件触发器来设置一个按钮来搜索网站:

<button onclick="myFunction()">Search</button>
搜索
我把你的代码写进了一个函数

我希望这对你有帮助

最后一个国家:

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>

<body>
  <input type="text" id="modulo" class="form">
  <button onclick="myFunction()">Search</button>
</body>

搜寻

var-link=[
"http://www.forumfree.it/",
"http://www.forumcommunity.net/",
"http://www.blogfree.net/",
];
函数myFunction(){
var输入=document.getElementById(“模”);
var结果=[];
对于(变量i=0;i-1){
结果:推送(链接[i]);
}
}
如果(results.length==1){
警报(“您的站点为:“+results[0]);
} 
否则如果(results.length>1){
警报(“抱歉,您的搜索返回多个结果:“+结果”)
}
否则{
警报(“对不起,我找不到:”+input.value)
}

}

我认为你的思路是对的。你只需要一些东西:

  • 启动搜索的按钮
  • 将代码放入函数中,以便调用它
  • 您想在数组
    链接中搜索输入
    模的值
函数findIt(){
var modulo=document.getElementById(“modulo”).value;
变量链接=[
"http://www.forumfree.it/",
"http://www.forumcommunity.net/",
"http://www.blogfree.net/",
];
if(link.indexOf(modulo)>-1){
警报(“您的站点为:”+模);
}否则{
警报(“对不起,我找不到:”+模)
}
}

数组中的搜索元素

我认为你的思路是对的。你只需要一些东西:

  • 启动搜索的按钮
  • 将代码放入函数中,以便调用它
  • 您想在数组
    链接中搜索输入
    模的值
函数findIt(){
var modulo=document.getElementById(“modulo”).value;
变量链接=[
"http://www.forumfree.it/",
"http://www.forumcommunity.net/",
"http://www.blogfree.net/",
];
if(link.indexOf(modulo)>-1){
警报(“您的站点为:”+模);
}否则{
警报(“对不起,我找不到:”+模)
}
}

数组中的搜索元素
函数runscript(){
var modulo=document.getElementById(“modulo”).value;
变量链接=[
"http://www.forumfree.it/",
"http://www.forumcommunity.net/",
"http://www.blogfree.net/",
];
var=false;
对于(变量i=0;i-1){
警报(“您的站点是:“+link[i]);
发现=真;
} 
}
如果(!找到){
警报(“对不起,我找不到:”+模)
}
}
还有HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Search element in array</title>
</head>
<body>
<input type="text" id="modulo" class="form">
<input type="button" onclick="runscript()" value="search" />
</body>
</html>

数组中的搜索元素
您需要按下按钮进行搜索

函数runscript(){
var modulo=document.getElementById(“modulo”).value;
变量链接=[
"http://www.forumfree.it/",
"http://www.forumcommunity.net/",
"http://www.blogfree.net/",
];
var=false;
对于(变量i=0;i-1){
警报(“您的站点是:“+link[i]);
发现=真;
} 
}
如果(!找到){
警报(“对不起,我找不到:”+模)
}
}
还有HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Search element in array</title>
</head>
<body>
<input type="text" id="modulo" class="form">
<input type="button" onclick="runscript()" value="search" />
</body>
</html>

数组中的搜索元素

您需要按下按钮进行搜索

什么类型的输入将进入模运算
,但是?
type=“text”
,这是文本输入脚本放在哪里?我看不到任何关于提交输入或类似按钮的引用请让我澄清。您希望用户输入什么?你是否期待类似于
论坛
?你可能想调查一个事件来触发你的代码这样做,以及做@SulZ在他的回答中建议的事情。什么类型的输入会进入
type=“text”
,我是文本输入你把脚本放在哪里?我看不到任何关于提交输入或类似按钮的引用请让我澄清。您希望用户输入什么?你是否期待类似于
论坛
?你可能希望调查一个事件来触发你的代码这样做,以及按照@SulZ在回答中的建议去做。但是用户必须非常准确,缺少一个结束斜杠可能会