简体   繁体   English

如何使用 JavaScript / jQuery 选择/取消选择带有复选框的选择选项?

[英]How to select/unselect select options with checkboxes with JavaScript / jQuery?

I have almost no knowledge of JavaScript or jQuery.我几乎不了解 JavaScript 或 jQuery。

I need to select/unselect an option in a <select> where multiple options can be selected when a checkbox or button is clicked.我需要在<select>中选择/取消选择一个选项,其中单击复选框或按钮时可以选择多个选项。

The checkbox needs to select/unselect the option with the same value.复选框需要选择/取消选择具有相同值的选项。 My idea was something like this:我的想法是这样的:

 $(document).ready(function() { var input = $('#entry-select'); var checkboxes = $('.entrycheckbox'); checkboxes.click(function() { var element = $(this); var value = element.val(); input.val(value); }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" class="entrycheckbox" value="1"> <input type="checkbox" class="entrycheckbox" value="2"> <input type="checkbox" class="entrycheckbox" value="3"> <form action=""> <select name="entries" id="entry-select" multiple> <option value="1">Option1</option> <option value="2">Option2</option> <option value="3">Option3</option> </select> </form>

This only selects the option with the value of the last clicked checkbox, not which ones are checked, and it unselects every other option.这仅选择具有上次单击的复选框的值的选项,而不是选中哪些选项,并且会取消选择所有其他选项。

You only give val() the value of the checkbox which was selected last.你只给val()最后选择的复选框的值。 To make this work as you require you need to build an array of all selected checkboxes and provide that to val() instead.要按照您的要求进行这项工作,您需要构建一个包含所有选定复选框的数组,并将其提供给val()

To achieve this you can use filter() to get the selected checkboxes, then map() to build the array:为此,您可以使用filter()获取选定的复选框,然后使用map()构建数组:

input.val(checkboxes.filter(':checked').map((i, el) => el.value));

 $(document).ready(function() { var $input = $('#entry-select'); var $checkboxes = $('.entrycheckbox'); $checkboxes.click(function() { $input.val($checkboxes.filter(':checked').map((i, el) => el.value)); }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" class="entrycheckbox" value="1"> <input type="checkbox" class="entrycheckbox" value="2"> <input type="checkbox" class="entrycheckbox" value="3"> <form action=""> <select name="entries" id="entry-select" multiple> <option value="1">Option1</option> <option value="2">Option2</option> <option value="3">Option3</option> </select> </form>

You may also want to consider adding readonly to the select if you don't want the user to change the selected option directly.如果您不希望用户直接更改所选option您可能还需要考虑将readonly添加到select

 <script type="text/javascript">
        $(document).ready(function(){
            $('.entrycheckbox').click(function(){

                $(":entrycheckbox").each(function(){
                    if($(this).val()==1){     
                    $(this).attr("checked","checked");
                    }
                });
            });
        });

    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM