JavaScript下拉框带复选框和搜索的全选

huangapple go评论72阅读模式
英文:

Javascript select all on dropdown with checkbox and search

问题

Here is the translated content:

我有一个复选框下拉菜单,其中包含搜索和全选功能。它们都运作良好,但问题是,当我搜索时,我希望“全选”仅选择显示的复选框,当前在搜索后,如果我点击“全选”,它仍然选择了所有复选框。

这是我的HTML代码:

<div id="dropdownSearch" class="hidden bg-white rounded-lg shadow w-60" style="z-index: 999;">
    <div class="p-3">
        <label for="input-group-search" class="sr-only">搜索</label>
        <div class="relative">
            <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
                <svg class="w-5 h-5 text-gray-500 dark:text-gray-400" aria-hidden="true" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd"></path></svg>
            </div>
            <input type="text" id="myInput" onkeyup="filterFunction()" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5" placeholder="搜索用户">
        </div>
    </div>
    <div class="ml-4 flex items-center p-1 rounded hover:bg-gray-100">
        <input id="checkbox-item-14" onchange="selectAll()" type="checkbox" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500">
        <label for="checkbox-item-14" id="select_all" class="w-full ml-2 text-sm font-medium text-gray-900 rounded dark:text-gray-300">全选</label>
    </div>
    <ul id="getMerchant" class="h-48 px-3 pb-3 overflow-y-auto text-sm text-gray-700 dark:text-gray-200" aria-labelledby="dropdownSearchButton">
        @foreach($mids as $mid)
        <li>
            <div id="myDropdown" class="flex items-center p-2 rounded hover:bg-gray-100">
                <input id="merchant" name="merchant[]" type="checkbox" value="{{$mid->mid_id}}" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500">
                <label for="merchant-checkbox" id="merchant_name" class="w-full ml-2 text-sm font-medium text-gray-900 rounded">{{$mid->mid_name}}</label>
            </div>
        </li>
        @endforeach
    </ul>
</div>

这是我的JavaScript代码:

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

    function selectAll(){
        if($('#checkbox-item-14').is(':checked')){
            var ele = document.querySelectorAll("input[type=checkbox]");
            for(var i=0;i<ele.length;i++){
                ele[i].checked = true;
            }
        }else{
            var ele = document.querySelectorAll("input[type=checkbox]");
            for(var i=0;i<ele.length;i++){
                ele[i].checked = false;
            }
        }
    }
</script>

希望这对你有帮助。

英文:

I have a checkbox dropdown that has a search and select all.
They both work great, the thing is when i search, i would like the select all to only select all the displayed checkboxes, right now after a search and i click select all it still selects every box.

Here is my html

&lt;div id=&quot;dropdownSearch&quot; class=&quot;hidden bg-white rounded-lg shadow w-60&quot; style=&quot;z-index: 999;&quot;&gt;
&lt;div class=&quot;p-3&quot;&gt;
&lt;label for=&quot;input-group-search&quot; class=&quot;sr-only&quot;&gt;Search&lt;/label&gt;
&lt;div class=&quot;relative&quot;&gt;
&lt;div class=&quot;absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none&quot;&gt;
&lt;svg class=&quot;w-5 h-5 text-gray-500 dark:text-gray-400&quot; aria-hidden=&quot;true&quot; fill=&quot;currentColor&quot; viewBox=&quot;0 0 20 20&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
&lt;/div&gt;
&lt;input type=&quot;text&quot; id=&quot;myInput&quot; onkeyup=&quot;filterFunction()&quot; class=&quot;bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 &quot; placeholder=&quot;Search user&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;ml-4 flex items-center p-1 rounded hover:bg-gray-100&quot;&gt;
&lt;input id=&quot;checkbox-item-14&quot; onchange=&quot;selectAll()&quot; type=&quot;checkbox&quot; class=&quot;w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500&quot;&gt;
&lt;label for=&quot;checkbox-item-14&quot; id=&quot;select_all&quot; class=&quot;w-full ml-2 text-sm font-medium text-gray-900 rounded dark:text-gray-300&quot;&gt;Select All&lt;/label&gt;
&lt;/div&gt;
&lt;ul id=&quot;getMerchant&quot; class=&quot;h-48 px-3 pb-3 overflow-y-auto text-sm text-gray-700 dark:text-gray-200&quot; aria-labelledby=&quot;dropdownSearchButton&quot;&gt;
@foreach($mids as $mid)
&lt;li&gt;
&lt;div id=&quot;myDropdown&quot; class=&quot;flex items-center p-2 rounded hover:bg-gray-100 &quot;&gt;
&lt;input id=&quot;merchant&quot; name=&quot;merchant[]&quot; type=&quot;checkbox&quot; value=&quot;{{$mid-&gt;mid_id}}&quot; class=&quot;w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 &quot;&gt;
&lt;label for=&quot;merchant-checkbox&quot; id=&quot;merchant_name&quot; class=&quot;w-full ml-2 text-sm font-medium text-gray-900 rounded &quot;&gt;{{$mid-&gt;mid_name}}&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
@endforeach
&lt;/ul&gt;

</div>

Here is my Javascript

&lt;script&gt;
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById(&quot;myInput&quot;);
filter = input.value.toUpperCase();
console.log(filter);
div = document.getElementById(&quot;getMerchant&quot;);
a = div.getElementsByTagName(&quot;li&quot;);
for (i = 0; i &lt; a.length; i++) {
txtValue = a[i].textContent || a[i].innerText;
if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
a[i].style.display = &quot;&quot;;
} else {
a[i].style.display = &quot;none&quot;;
}
}
}
function selectAll(){
if($(&#39;#checkbox-item-14&#39;).is(&#39;:checked&#39;)){
var ele = document.querySelectorAll(&quot;input[type=checkbox]&quot;);
for(var i=0;i&lt;ele.length;i++){
ele[i].checked = true;
}
}else{
var ele = document.querySelectorAll(&quot;input[type=checkbox]&quot;);
for(var i=0;i&lt;ele.length;i++){
ele[i].checked = false;
}
}
}
&lt;/script&gt;

答案1

得分: 0

为了在搜索时保留全选选项,请考虑从索引1开始循环for,这样可以跳过第一个&lt;li&gt;,包含全选选项的&lt;li&gt;

for (i = 1; i &lt; a.length; i++) {

要使全选仅对当前筛选的项目起作用,请首先使用hidden类而不是使用style属性。这样可以更容易地不选择隐藏的项目:

a[i].classList.toggle(&#39;hidden&#39;, txtValue.toUpperCase().indexOf(filter) === -1);

然后在切换全选复选框时,选择不隐藏的&lt;input&gt;元素:

var ele = document.querySelectorAll(&quot;li:not(.hidden) &gt; div &gt; input[type=checkbox]&quot;);

将所有内容整合在一起:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

function filterFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById(&quot;myInput&quot;);
  filter = input.value.toUpperCase();
  console.log(filter);
  div = document.getElementById(&quot;getMerchant&quot;);
  a = div.getElementsByTagName(&quot;li&quot;);
  for (i = 1; i &lt; a.length; i++) {
    txtValue = a[i].textContent || a[i].innerText;
    a[i].classList.toggle(&#39;hidden&#39;, txtValue.toUpperCase().indexOf(filter) === -1);
  }
}

function selectAll() {
  var ele = document.querySelectorAll(&quot;li:not(.hidden) &gt; div &gt; input[type=checkbox]&quot;);
  var checked = $(&#39;#checkbox-item-14&#39;).is(&#39;:checked&#39;);
  for (var i = 0; i &lt; ele.length; i++) {
    ele[i].checked = checked;
  }
}

<!-- language: lang-html -->

<script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js&quot; integrity=&quot;sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
<script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;

<div id=&quot;dropdownSearch&quot; class=&quot;bg-white rounded-lg shadow w-60&quot; style=&quot;z-index: 999;&quot;&gt;
  <div class=&quot;p-3&quot;&gt;
    <label for=&quot;input-group-search&quot; class=&quot;sr-only&quot;&gt;Search&lt;/label&gt;
    <div class=&quot;relative&quot;&gt;
      <div class=&quot;absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none&quot;&gt;
        <svg class=&quot;w-5 h-5 text-gray-500 dark:text-gray-400&quot; aria-hidden=&quot;true&quot; fill=&quot;currentColor&quot; viewBox=&quot;0 0 20 20&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
      </div&gt;
      <input type=&quot;text&quot; id=&quot;myInput&quot; onkeyup=&quot;filterFunction()&quot; class=&quot;bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 &quot; placeholder=&quot;Search user&quot;&gt;
    </div&gt;
  </div&gt;
  <ul id=&quot;getMerchant&quot; class=&quot;h-48 px-3 pb-3 overflow-y-auto text-sm text-gray-700 dark:text-gray-200&quot; aria-labelledby=&quot;dropdownSearchButton&quot;&gt;
    <li&gt;
      <div class=&quot;flex items-center p-2 rounded hover:bg-gray-100 dark:hover:bg-gray-600&quot;&gt;
        <input id=&quot;checkbox-item-14&quot; onchange=&quot;selectAll()&quot; type=&quot;checkbox&quot; class=&quot;w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500&quot;&gt;
        <label for=&quot;checkbox-item-14&quot; id=&quot;select_all&quot; class=&quot;w-full ml-2 text-sm font-medium text-gray-900 rounded dark:text-gray-300&quot;&gt;Select All&lt;/label&gt;
      </div&gt;
    </li&gt;
    <li&gt;
      <div id=&quot;myDropdown&quot; class=&quot;flex items-center p-2 rounded hover-bg-gray-100 &quot;&gt;
        <input id=&quot;merchant&quot; name=&quot;merchant[]&quot; type=&quot;checkbox&quot; value=&quot;foo&quot; class=&quot;w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 &quot;&gt;
        <label for=&quot;merchant-checkbox&quot; id=&quot;merchant_name&quot; class=&quot;w-full ml-2 text-sm font-medium text-gray-900 rounded &quot;&gt;foo&lt;/label&gt;
      </div&gt;
    </li&gt;
    <li&gt;
      <div id=&quot;myDropdown&quot; class=&quot;flex items-center p-2 rounded hover-bg-gray-100 &quot;&gt;
        <input id=&quot;merchant&quot; name=&quot;merchant[]&quot; type=&quot;checkbox&quot; value=&quot;bar&quot; class=&quot;w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 &quot;&gt;
        <label for=&

<details>
<summary>英文:</summary>

To keep the *Select All* option when searching, consider starting the `for` loop from index `1`, so that it skips the first `&lt;li&gt;`, the `&lt;li&gt;` that contains the *Select All* option:

```js
for (i = 1; i &lt; a.length; i++) {

For the Select All to only work for the currently filtered items, first, use the hidden class instead of using the style attribute. This will make it easier to not select the hidden items:

a[i].classList.toggle(&#39;hidden&#39;, txtValue.toUpperCase().indexOf(filter) === -1);

Then when toggling the Select All checkbox, select checkbox &lt;input&gt; elements that are not hidden:

var ele = document.querySelectorAll(&quot;li:not(.hidden) &gt; div &gt; input[type=checkbox]&quot;);

Putting it all together:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById(&quot;myInput&quot;);
filter = input.value.toUpperCase();
console.log(filter);
div = document.getElementById(&quot;getMerchant&quot;);
a = div.getElementsByTagName(&quot;li&quot;);
for (i = 1; i &lt; a.length; i++) {
txtValue = a[i].textContent || a[i].innerText;
a[i].classList.toggle(&#39;hidden&#39;, txtValue.toUpperCase().indexOf(filter) === -1);
}
}
function selectAll() {
var ele = document.querySelectorAll(&quot;li:not(.hidden) &gt; div &gt; input[type=checkbox]&quot;);
var checked = $(&#39;#checkbox-item-14&#39;).is(&#39;:checked&#39;);
for (var i = 0; i &lt; ele.length; i++) {
ele[i].checked = checked;
}
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js&quot; integrity=&quot;sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;dropdownSearch&quot; class=&quot;bg-white rounded-lg shadow w-60&quot; style=&quot;z-index: 999;&quot;&gt;
&lt;div class=&quot;p-3&quot;&gt;
&lt;label for=&quot;input-group-search&quot; class=&quot;sr-only&quot;&gt;Search&lt;/label&gt;
&lt;div class=&quot;relative&quot;&gt;
&lt;div class=&quot;absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none&quot;&gt;
&lt;svg class=&quot;w-5 h-5 text-gray-500 dark:text-gray-400&quot; aria-hidden=&quot;true&quot; fill=&quot;currentColor&quot; viewBox=&quot;0 0 20 20&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
&lt;/div&gt;
&lt;input type=&quot;text&quot; id=&quot;myInput&quot; onkeyup=&quot;filterFunction()&quot; class=&quot;bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 &quot; placeholder=&quot;Search user&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;ul id=&quot;getMerchant&quot; class=&quot;h-48 px-3 pb-3 overflow-y-auto text-sm text-gray-700 dark:text-gray-200&quot; aria-labelledby=&quot;dropdownSearchButton&quot;&gt;
&lt;li&gt;
&lt;div class=&quot;flex items-center p-2 rounded hover:bg-gray-100 dark:hover:bg-gray-600&quot;&gt;
&lt;input id=&quot;checkbox-item-14&quot; onchange=&quot;selectAll()&quot; type=&quot;checkbox&quot; class=&quot;w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500&quot;&gt;
&lt;label for=&quot;checkbox-item-14&quot; id=&quot;select_all&quot; class=&quot;w-full ml-2 text-sm font-medium text-gray-900 rounded dark:text-gray-300&quot;&gt;Select All&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div id=&quot;myDropdown&quot; class=&quot;flex items-center p-2 rounded hover:bg-gray-100 &quot;&gt;
&lt;input id=&quot;merchant&quot; name=&quot;merchant[]&quot; type=&quot;checkbox&quot; value=&quot;foo&quot; class=&quot;w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 &quot;&gt;
&lt;label for=&quot;merchant-checkbox&quot; id=&quot;merchant_name&quot; class=&quot;w-full ml-2 text-sm font-medium text-gray-900 rounded &quot;&gt;foo&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div id=&quot;myDropdown&quot; class=&quot;flex items-center p-2 rounded hover:bg-gray-100 &quot;&gt;
&lt;input id=&quot;merchant&quot; name=&quot;merchant[]&quot; type=&quot;checkbox&quot; value=&quot;bar&quot; class=&quot;w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 &quot;&gt;
&lt;label for=&quot;merchant-checkbox&quot; id=&quot;merchant_name&quot; class=&quot;w-full ml-2 text-sm font-medium text-gray-900 rounded &quot;&gt;bar&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div id=&quot;myDropdown&quot; class=&quot;flex items-center p-2 rounded hover:bg-gray-100 &quot;&gt;
&lt;input id=&quot;merchant&quot; name=&quot;merchant[]&quot; type=&quot;checkbox&quot; value=&quot;baz&quot; class=&quot;w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 &quot;&gt;
&lt;label for=&quot;merchant-checkbox&quot; id=&quot;merchant_name&quot; class=&quot;w-full ml-2 text-sm font-medium text-gray-900 rounded &quot;&gt;baz&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 04:03:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定