2017-05-03 3 views
0

私は普通のHTMlでコンボボックスを作ろうとしています。 2つのdivのように、searchboxの最初のdivとリストの2番目のdiv。私は、検索ボックスをクリックして表示されるか、コンボボックスのように表示されたり表示されたりしたい。検索ボックスのクリック時にリストdivを作る方法

コード。

<!DOCTYPE html> 
<html> 
<head> 
<style> 
* { 
    box-sizing: border-box; 
} 
#myInput { 
    background-image: url('/css/searchicon.png'); 
    background-position: 10px 10px; 
    background-repeat: no-repeat; 
    width: 100%; 
    font-size: 16px; 
    padding: 12px 20px 12px 40px; 
    border: 1px solid #ddd; 
    margin-bottom: 12px; 
} 

#myTable { 
    border-collapse: collapse; 
    width: 100%; 
    border: 1px solid #ddd; 
    font-size: 18px; 
} 

#myTable th, #myTable td { 
    text-align: left; 
    padding: 12px; 
} 

#myTable tr { 
    border-bottom: 1px solid #ddd; 
} 

#myTable tr.header, #myTable tr:hover { 
    background-color: #f1f1f1; 
} 
</style> 
</head> 
<body> 

<h2>My Customers</h2> 

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name"> 

<table id="myTable"> 
    <tr class="header"> 
    <th style="width:60%;">Name</th> 
    <th style="width:40%;">Country</th> 
    </tr> 
    <tr> 
    <td>Germany</td> 
    </tr> 
    <tr> 
    <td>Sweden</td> 
    </tr> 
    <tr> 
    <td>UK</td> 
    </tr> 
    <tr> 
    <td>Germany</td> 
    </tr> 
    <tr> 
    <td>Canada</td> 
    </tr> 
    <tr> 
    <td>Italy</td> 
    </tr> 
    <tr> 
    <td>UK</td> 
    </tr> 
    <tr> 
    <td>France</td> 
    </tr> 
</table> 

<script> 
function myFunction() { 
    var input, filter, table, tr, td, i; 
    input = document.getElementById("myInput"); 
    filter = input.value.toUpperCase(); 
    table = document.getElementById("myTable"); 
    tr = table.getElementsByTagName("tr"); 
    for (i = 0; i < tr.length; i++) { 
    td = tr[i].getElementsByTagName("td")[0]; 
    if (td) { 
     if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { 
     tr[i].style.display = ""; 
     } else { 
     tr[i].style.display = "none"; 
     } 
    }  
    } 
} 
</script> 

</body> 
</html> 

searchboxとlistを別のdivやリストに統合するには、comboboxのような検索ボックスをクリックするだけで表示されます。

+0

あなたはユーザーが検索ボックスをクリックしてリストを聞かせたときに要素を非表示にしますか? –

+0

はい...これはコンボボックスのようなものです。また、ユーザーはリストから選択してコンボボックスに表示することもできます。 – David

答えて

1

はあなたのCSSこの

更新を達成するためにのみCSSを使用することができます

#myInput:focus + #myTable{ 
    display: block; // Show table on focus 
} 

#myTable:hover{ 
display: block; 
} 

UPDATE:

は、次のJS

document.querySelectorAll('#myTable tr:not(.header)').forEach(function(_tr){ 
    _tr.addEventListener('click',function(){ 
     var selection = this.getElementsByTagName('td')[0].textContent; //current selection  
     var selections = document.getElementById('myInput').value; //Old Selections 

     if(selections !="") selections +=","; //add seperator if selections is not empty. 
     if(selections.indexOf(selection)!=-1) return; //if selection already exist return. 

     selections+= selection; //Append selection to selections 
     document.getElementById('myInput').value = selections; 
    }); 
}); 
を使用する複数の選択のためにリスト

document.querySelectorAll('#myTable tr:not(.header)').forEach(function(_tr){ 
    _tr.addEventListener('click',function(){ 
       document.getElementById('myInput').value = this.getElementsByTagName('td')[0].textContent; 
    }); 
}); 

UPDATE-2

からオプションを選択するために、次のJSを追加します。

スニペットはこちら

<html> 
 
<head> 
 
<style> 
 
* { 
 
    box-sizing: border-box; 
 
} 
 
#myInput { 
 
    background-image: url('/css/searchicon.png'); 
 
    background-position: 10px 10px; 
 
    background-repeat: no-repeat; 
 
    width: 100%; 
 
    font-size: 16px; 
 
    padding: 12px 20px 12px 40px; 
 
    border: 1px solid #ddd; 
 
    margin-bottom: 12px; 
 
} 
 

 
#myTable { 
 
    border-collapse: collapse; 
 
    width: 100%; 
 
    border: 1px solid #ddd; 
 
    font-size: 18px; 
 
    display: none; 
 
    margin-top:-12px; 
 
} 
 

 
#myTable th, #myTable td { 
 
    text-align: left; 
 
    padding: 12px; 
 
} 
 

 
#myTable tr { 
 
    border-bottom: 1px solid #ddd; 
 
} 
 

 
#myTable tr.header, #myTable tr:hover { 
 
    background-color: #f1f1f1; 
 
} 
 

 
#myInput:focus + #myTable{ 
 
    display: block; 
 
} 
 

 
#myTable:hover{ 
 
display: block; 
 
} 
 
</style> 
 
</head> 
 
<body> 
 

 
<h2>My Customers</h2> 
 

 
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name"> 
 

 
<table id="myTable"> 
 
    <tr class="header"> 
 
    <th style="width:60%;">Name</th> 
 
    <th style="width:40%;">Country</th> 
 
    </tr> 
 
    <tr> 
 
    <td>Germany</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Sweden</td> 
 
    </tr> 
 
    <tr> 
 
    <td>UK</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Germany</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Canada</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Italy</td> 
 
    </tr> 
 
    <tr> 
 
    <td>UK</td> 
 
    </tr> 
 
    <tr> 
 
    <td>France</td> 
 
    </tr> 
 
</table> 
 

 
<script> 
 
function myFunction() { 
 
    var input, filter, table, tr, td, i; 
 
    input = document.getElementById("myInput"); 
 
    filter = input.value.toUpperCase(); 
 
    table = document.getElementById("myTable"); 
 
    tr = table.getElementsByTagName("tr"); 
 
    for (i = 0; i < tr.length; i++) { 
 
    td = tr[i].getElementsByTagName("td")[0]; 
 
    if (td) { 
 
     if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { 
 
     tr[i].style.display = ""; 
 
     } else { 
 
     tr[i].style.display = "none"; 
 
     } 
 
    }  
 
    } 
 
} 
 

 
document.querySelectorAll('#myTable tr:not(.header)').forEach(function(_tr){ 
 
    _tr.addEventListener('click',function(){ 
 
var selection = this.getElementsByTagName('td')[0].textContent;   
 
var selections = document.getElementById('myInput').value; 
 
     if(selections !="") selections +=","; 
 
     if(selections.indexOf(selection)!=-1) return; 
 
     selections+= selection; 
 
    \t \t \t document.getElementById('myInput').value = selections; 
 
\t }); 
 
}); 
 
</script> 
 

 
</body> 
 
</html>

+0

はい、ありがとうございますが、選択はここでは起こっていません。 – David

+0

どのような選択? –

+0

達成しようとしていることを説明できますか? –

0

このような意味ですか?次CSSを追加

#myTable { 
    border-collapse: collapse; 
    width: 100%; 
    border: 1px solid #ddd; 
    font-size: 18px; 
    margin-top: -12px; // Remove space between table and input 
    display: none; // Hide table by default 
} 

として

 <!DOCTYPE html> 
    <html> 
    <head> 
    <style> 
    * { 
     box-sizing: border-box; 
    } 
    #myInput { 
     //background-image: url('/css/searchicon.png'); 
     background-position: 10px 10px; 
     background-repeat: no-repeat; 
     width: 100%; 
     font-size: 16px; 
     padding: 12px 20px 12px 40px; 
     border: 1px solid #ddd; 
     margin-bottom: 12px; 
    } 

    #myTable { 
     border-collapse: collapse; 
     width: 100%; 
     border: 1px solid #ddd; 
     font-size: 18px; 
    } 

    #myTable th, #myTable td { 
     text-align: left; 
     padding: 12px; 
    } 

    #myTable tr { 
     border-bottom: 1px solid #ddd; 
    } 

    #myTable tr.header, #myTable tr:hover { 
     background-color: #f1f1f1; 
    } 
    </style> 
    </head> 
    <body> 

    <h2>My Customers</h2> 

    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name"> 
    <div id="searchResult"> 
     <form> 
      <select name="search" id="search" style="display: none;"> 

      </select> 
     </form> 
    </div> 

    <table id="myTable"> 
     <tr class="header"> 
     <th style="width:60%;">Name</th> 
     <th style="width:40%;">Country</th> 
     </tr> 
     <tr> 
     <td>Germany</td> 
     </tr> 
     <tr> 
     <td>Sweden</td> 
     </tr> 
     <tr> 
     <td>UK</td> 
     </tr> 
     <tr> 
     <td>Germany</td> 
     </tr> 
     <tr> 
     <td>Canada</td> 
     </tr> 
     <tr> 
     <td>Italy</td> 
     </tr> 
     <tr> 
     <td>UK</td> 
     </tr> 
     <tr> 
     <td>France</td> 
     </tr> 
    </table> 

    <script> 
    function myFunction() { 
     var input, filter, table, tr, td, i, searchBox; 
     input = document.getElementById("myInput"); 
     filter = input.value.toUpperCase(); 
     table = document.getElementById("myTable"); 
     tr = table.getElementsByTagName("tr"); 
     searchBox = document.getElementById("search"); 

     var option = document.createElement("option"); 

     var length = searchBox.options.length; 
     for (i = 0; i < length; i++) { 
      searchBox.options[i] = null; 
     } 
     if(filter === ""){ 

      document.getElementById("search").style.display = "none"; 
     } 
     else { 
      for (i = 0; i < tr.length; i++) { 
      td = tr[i].getElementsByTagName("td")[0]; 
      if (td) { 
       if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { 
       //tr[i].style.display = "";    
       option.text = option.value = td.innerHTML.toUpperCase(); 
       searchBox.add(option); 
       } else { 
       //tr[i].style.display = "none"; 
       } 
      }  
      } 
      var length = searchBox.options.length; 
      if(length === 0) { 
       document.getElementById("search").style.display = "none"; 
      } 
      else { 
       document.getElementById("search").style.display = "block"; 
      } 


     } 



    } 
    </script> 

    </body> 
    </html> 
0

<!DOCTYPE html> 
 
    <html> 
 
    <head> 
 
    <style> 
 
    * { 
 
     box-sizing: border-box; 
 
    } 
 
    #myInput { 
 
     //background-image: url('/css/searchicon.png'); 
 
     background-position: 10px 10px; 
 
     background-repeat: no-repeat; 
 
     width: 100%; 
 
     font-size: 16px; 
 
     padding: 12px 20px 12px 40px; 
 
     border: 1px solid #ddd; 
 
     margin-bottom: 12px; 
 
    } 
 

 
    #myTable { 
 
     border-collapse: collapse; 
 
     width: 100%; 
 
     border: 1px solid #ddd; 
 
     font-size: 18px; 
 
    } 
 

 
    #myTable th, #myTable td { 
 
     text-align: left; 
 
     padding: 12px; 
 
    } 
 

 
    #myTable tr { 
 
     border-bottom: 1px solid #ddd; 
 
    } 
 

 
    #myTable tr.header, #myTable tr:hover { 
 
     background-color: #f1f1f1; 
 
    } 
 
    </style> 
 
    </head> 
 
    <body> 
 
    <h2>My Customers</h2> 
 
    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name"> 
 
    <div id="searchResult"> 
 
     <form> 
 
      <select name="SearchThis" id="SearchThis" style="display: none;"> 
 

 
      </select> 
 
     </form> 
 
    </div> 
 
    <table id="myTable"> 
 
     <tr class="header"> 
 
     <th style="width:60%;">Name</th> 
 
     <th style="width:40%;">Country</th> 
 
     </tr> 
 
     <tr> 
 
     <td>Germany</td> 
 
     </tr> 
 
     <tr> 
 
     <td>Sweden</td> 
 
     </tr> 
 
     <tr> 
 
     <td>UK</td> 
 
     </tr> 
 
     <tr> 
 
     <td>Germany</td> 
 
     </tr> 
 
     <tr> 
 
     <td>Canada</td> 
 
     </tr> 
 
     <tr> 
 
     <td>Italy</td> 
 
     </tr> 
 
     <tr> 
 
     <td>UK</td> 
 
     </tr> 
 
     <tr> 
 
     <td>France</td> 
 
     </tr> 
 
    </table> 
 

 
    <script> 
 
    function myFunction() { 
 
     var input, filter, table, tr, td, i, searchBox; 
 
     input = document.getElementById("myInput"); 
 
     filter = input.value.toUpperCase(); 
 
     table = document.getElementById("myTable"); 
 
     tr = table.getElementsByTagName("tr"); 
 
     searchBox = document.getElementById("SearchThis"); 
 

 
     var option = document.createElement("option"); 
 

 
     var length = searchBox.options.length; 
 
     for (i = 0; i < length; i++) { 
 
      searchBox.options[i] = null; 
 
     } 
 
     if(filter === "") 
 
     { 
 

 
      document.getElementById("SearchThis").style.display = "none"; 
 
     } 
 
     else 
 
     { 
 
      for (i = 0; i < tr.length; i++) { 
 
      td = tr[i].getElementsByTagName("td")[0]; 
 
      if (td) { 
 
       if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { 
 
       //tr[i].style.display = "";    
 
       option.text = option.value = td.innerHTML.toUpperCase(); 
 
       searchBox.add(option); 
 
       } else { 
 
       //tr[i].style.display = "none"; 
 
       } 
 
      }  
 
      } 
 
      var length = searchBox.options.length; 
 
      if(length === 0) 
 
      { 
 
       document.getElementById("SearchThis").style.display = "none"; 
 
      } 
 
      else 
 
      { 
 
       document.getElementById("SearchThis").style.display = "block"; 
 
      } 
 
     } 
 
    } 
 
    </script> 
 
    </body> 
 
    </html>

これはあなたの欲求の出力であるように!それを確認して教えてください!

関連する問題