创建具有多选功能的下拉菜单,使用JS。

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

create dropdown with multiple selection in JS

问题

我想要实现一个下拉菜单,类似于Booking.com的功能(我附上了一个截图),但我遇到了一些问题,无法确定出错在哪里。您有什么建议吗?

HTML

<div class="dropdown">
    <input type="text" id="droptxt" class="list" readonly placeholder="Number of guests">
    <div id="content" class="content">
        <div class="list">
            <input type="checkbox" id="rooms" class="list" value="Choose how many rooms" />
            <label for="Choose how many rooms" class="list">Choose how many rooms </label>
            <input type="hidden" class="list quantity" min="1" value="1" />
        </div>
        <div class="list">
            <input type="checkbox" id="adults" class="list" value="Choose the number of adults" />
            <label for="Choose the number of adults" class="list">Choose the number of adults </label>
            <input type="hidden" class="list quantity" min="1" value="1" />
        </div>
        <div class="list">
            <input type="checkbox" id="children" class="list" value="Choose the number of children" />
            <label for="Choose the number of children" class="list">Choose the number of children </label>
            <input type="hidden" class="list quantity" min="1" value="1" />
        </div>
    </div>
</div>

JavaScript

const txt = document.getElementById('droptxt');
console.log(txt);
const content = document.getElementById('content');
console.log(content);
const checkboxes = document.querySelectorAll('.list input[type="checkbox"]');
const quantity = document.querySelectorAll('.list input[type="number"]');

txt.addEventListener('click', function() {
    content.classList.toggle('show');
});

// Close the dropdown if the user clicks outside of it
window.onclick = function(e) {
    if (!e.target.matches('.list')) {
        if (content.classList.contains('show')) content.classList.remove('show');
    }
};

checkboxes.forEach(function(checkbox, index) {
    checkbox.addEventListener('click', function() {
        quantity[index].type = (checkbox.checked) ? 'number' : 'hidden';
        calc();
    });
});

quantity.forEach(function(input) {
    input.addEventListener('input', calc);
});

function calc() {
    let arr = [];
    for (let i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].checked) {
            arr.push(quantity[i].value + ' x ' + checkboxes[i].value);
        }
    }

    txt.value = arr.join(', ');
}

请注意,这是提供的代码和文本的翻译。如果您需要进一步的帮助或解释,请随时提出。

英文:

I want to implement a dropdown similar to the one on Booking.com in terms of functionality (I attach a screenshot), but I am encountering some issues and I can't figure out where I'm going wrong. Do you have any suggestions?
创建具有多选功能的下拉菜单,使用JS。

HTML

                                &lt;div class=&quot;dropdown&quot;&gt;
                                &lt;input type=&quot;text&quot; id=&quot;droptxt&quot; class=&quot;list&quot; readonly placeholder=&quot;Number of guests&quot;&gt;
                                &lt;div id=&quot;content&quot; class=&quot;content&quot;&gt;
                                    &lt;div class=&quot;list&quot;&gt;
                                        &lt;input type=&quot;checkbox&quot; id=&quot;rooms&quot; class=&quot;list&quot; value=&quot;Choose how many rooms&quot; /&gt;
                                        &lt;label for=&quot;Choose how many rooms&quot; class=&quot;list&quot;&gt;Choose how many rooms &lt;/label&gt;
                                        &lt;input type=&quot;hidden&quot; class=&quot;list quantity&quot; min=&quot;1&quot; value=&quot;1&quot; /&gt;
                                    &lt;/div&gt;
                                    &lt;div class=&quot;list&quot;&gt;
                                        &lt;input type=&quot;checkbox&quot; id=&quot;adults&quot; class=&quot;list&quot; value=&quot;Choose the number of adults&quot; /&gt;
                                        &lt;label for=&quot;Choose the number of adults&quot; class=&quot;list&quot;&gt;Choose the number of adults &lt;/label&gt;
                                        &lt;input type=&quot;hidden&quot; class=&quot;list quantity&quot; min=&quot;1&quot; value=&quot;1&quot; /&gt;
                                    &lt;/div&gt;
                                    &lt;div class=&quot;list&quot;&gt;
                                        &lt;input type=&quot;checkbox&quot; id=&quot;children&quot; class=&quot;list&quot; value=&quot;Choose the number of children&quot; /&gt;
                                        &lt;label for=&quot;Choose the number of children&quot; class=&quot;list&quot;&gt;Choose the number of children &lt;/label&gt;
                                        &lt;input type=&quot;hidden&quot; class=&quot;list quantity&quot; min=&quot;1&quot; value=&quot;1&quot; /&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;

JavaScript

const txt = document.getElementById(&#39;droptxt&#39;);
console.log(txt);
const content = document.getElementById(&#39;content&#39;);
console.log(content);
const checkboxes = document.querySelectorAll(&#39;.list input[type=&quot;checkbox&quot;]&#39;);
const quantity = document.querySelectorAll(&#39;.list input[type=&quot;number&quot;]&#39;);

txt.addEventListener(&#39;click&#39;, function() {
    content.classList.toggle(&#39;show&#39;);
});

// Close the dropdown if the user clicks outside of it
window.onclick = function(e) {
    if (!e.target.matches(&#39;.list&#39;)) {
        if (content.classList.contains(&#39;show&#39;)) content.classList.remove(&#39;show&#39;);
    }
};

checkboxes.forEach(function(checkbox, index) {
    checkbox.addEventListener(&#39;click&#39;, function() {
        quantity[index].type = (checkbox.checked) ? &#39;number&#39; : &#39;hidden&#39;;
        calc();
    });
});

quantity.forEach(function(input) {
    input.addEventListener(&#39;input&#39;, calc);
});

function calc() {
    let arr = [];
    for (let i = 0; i &lt; checkboxes.length; i++) {
        if (checkboxes[i].checked) {
            arr.push(quantity[i].value + &#39; x &#39; + checkboxes[i].value);
        }
    }

    txt.value = arr.join(&#39;, &#39;);
}

答案1

得分: 0

在这一行中,你选择了input[type=&quot;number&quot;],但在你的HTML中没有类型为"number"的输入。使用这个代码来解决问题:

const quantity = document.querySelectorAll('.list input[type="hidden"]');

这将解决你的问题。

英文:
 const quantity = document.querySelectorAll(&#39;.list input[type=&quot;number&quot;]&#39;); 

in this line you are selecting input[type="number"] but in your html there is no input which type is number. use this

 const quantity = document.querySelectorAll(&#39;.list input[type=&quot;hidden&quot;]&#39;); 

it will solve your problem

huangapple
  • 本文由 发表于 2023年2月18日 01:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487239.html
匿名

发表评论

匿名网友

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

确定