Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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_Php_Html - Fatal编程技术网

Javascript 清除搜索字段时如何隐藏项目?

Javascript 清除搜索字段时如何隐藏项目?,javascript,php,html,Javascript,Php,Html,我想在清除搜索字段时隐藏项目,但在搜索项目并清除搜索字段时,所有项目都将可见 我试过了 if(input.value.length == 0){ a[i].style.display = "none"; return; }else{ a[i].style.display = "block"; } 但这会产生一个错误: index.php:160unc

我想在清除搜索字段时隐藏项目,但在搜索项目并清除搜索字段时,所有项目都将可见

我试过了

 if(input.value.length == 0){
                a[i].style.display = "none";
                return;
            }else{
                a[i].style.display = "block";
            }
但这会产生一个错误:

index.php:160
uncaughttypeerror:无法读取未定义的属性“style” 在filterFunction(
index.php:160
) 在HTMLInputElement.onkeyup(
index.php:125

第125行:

第160行:
filter=input.value.toUpperCase()

代码如下:

      <div class="dropdown">
                            <div id="myDropdown" class="dropdown-content">
                                <input type="text" placeholder="Search.." 
        id="myInput" onkeyup="filterFunction()">
                                <a href="Index.html">Home</a>
                                <a href="">Refurbish Desktop</a>
                                <a href="">Refurbish Laptop</a>
                                <a href="">Refurbish Monitoren</a>
                                <a href="">Refurbish Printers</a>
                                <a href="">Groothandel Desktop</a>
                                <a href="">Groothandel Laptop</a>
                                <a href="">Groothandel Monitoren</a>
                                <a href="">Groothandel Printers</a>
                                <a href="">Recycling Desktop</a>
                                <a href="">Recycling Laptop</a>
                                <a href="">Recycling Thin Client</a>
                                <a href="">Recycling Monitoren</a>
                                <a href="">Recycling Printers</a>
                                <a href="">Werkproces HP Folio 9470m</a>
                                <a href="">QA Refurbish Desktop</a>
                                <a href="">QA Refurbish Laptop</a>
                                <a href="">QA Refurbish Monitoren</a>
                                <a href="">QA Refurbish Printers</a>
                            </div>
                        </div>

              <script>

                    function filterFunction() {
                      var input, filter, a, i;
                      input = document.getElementById("myInput");
                      filter = input.value.toUpperCase();
                      div = document.getElementById("myDropdown");
                      a = div.getElementsByTagName("a");
                      if(input.value.length == 0){
                        a[i].style.display = "none";
                        return;
                    }else{
                        a[i].style.display = "block";
                    }
                      for (i = 0; i < a.length; i++) {
                        txtValue = a[i].textContent || a[i].innerText;
                        if (txtValue.toUpperCase().indexOf(filter) > -1) {
                          a[i].style.display = "block";
                        } else {
                          a[i].style.display = "none";
                        }
                      }
                    }
                    </script>


函数filterFunction(){
变量输入,过滤器,a,i;
输入=document.getElementById(“myInput”);
filter=input.value.toUpperCase();
div=document.getElementById(“myDropdown”);
a=div.getElementsByTagName(“a”);
if(input.value.length==0){
a[i].style.display=“无”;
返回;
}否则{
a[i].style.display=“block”;
}
对于(i=0;i-1){
a[i].style.display=“block”;
}否则{
a[i].style.display=“无”;
}
}
}

我希望在清除字段时清除该字段

您第一次没有定义
I

试试这个:

if(input.value.length == 0){
     for(i=0;i<a.length;i++){
           a[i].style.display = "none";
     }
     return;
}
else{
     for(i=0;i<a.length;i++){
           a[i].style.display = "block";
     }
}
if(input.value.length==0){

对于(i=0;i您在输入标记上使用
onkeyup
事件。您是否可以尝试
onkeyup
而不是
onkeyup

<input type="text" placeholder="Search.." id="myInput" onkeydown="filterFunction()">


Hi@IstiaqueHossain老实说我不知道,这是我第一次使用java(javascript)谢谢,我会研究这个!Hi这很有效,非常感谢,我以为我已经为(i=0;i-1)定义了
i
Hi@ariferol01上面的评论修复了我的问题。不过还是谢谢你。