简体   繁体   English

如何在下拉列表/选择列表列表中添加复选框选中的项目

[英]How to add checkbox checked items in Dropdown/selectlist list

I am creating customer profile page in ASP.Net Core MVC.我正在 ASP.Net Core MVC 中创建客户资料页面。 In Customer profile I give list of available currencies (in checkboxes) from which we can choose in which currencies this customer can deal with us.在客户资料中,我给出了可用货币列表(在复选框中),我们可以从中选择该客户可以与我们交易的货币。

I want to set one default currency from checked checkboxes, for this I have created dropdown list and I want to fill the dropdown list with all the checked currencies, so that from this dropdown I will choose one default currency.我想从选中的复选框中设置一种默认货币,为此我创建了下拉列表,我想用所有选中的货币填充下拉列表,以便从这个下拉列表中选择一种默认货币。

Can some one provide Javascript to fill the dropdown list as per selected checkboxes.有人可以提供Javascript来根据选定的复选框填充下拉列表。

            @for (int j = 0; j < Model.Currency.Count(); j++)
            {
                <tr>
                    <td>
                        @Html.CheckBoxFor(m => m.Currency[j].Checked)
                    </td>
                    <td>
                        @Html.DisplayFor(m => m.Currency[j].Name)

                        @Html.HiddenFor(m => m.Currency[j].Id)
                        @Html.HiddenFor(m => m.Currency[j].Name)
                    </td>
                </tr>
            }
        </table>
      <div class="col-5">
            <select id="Default_Currency"
                    asp-for="DefaultCurrency"
                    asp-items=""
                    class="form-control">

            </select>
        </div>

According to your description, I suggest you could try to add the select options by using jquery.根据您的描述,我建议您可以尝试使用 jquery 添加选择选项。

More details, you could refer to below codes:更多细节,您可以参考以下代码:

<table>
        @for (int j = 0; j < Model.Currency.Count(); j++)
        {
            <tr>
                <td>
                    @Html.CheckBoxFor(m => m.Currency[j].Checked, new { @class = "ckbox" })
                </td>
                <td>
                    @Html.DisplayFor(m => m.Currency[j].Name)

                    @Html.HiddenFor(m => m.Currency[j].Id)
                    @Html.HiddenFor(m => m.Currency[j].Name)
                </td>
            </tr>
        }
</table>
<div class="col-5">
    <select id="Default_Currency"
            class="form-control">
    </select>
</div>


@section scripts{
    <script type="text/javascript">

        $(
            function () {
                $(".ckbox").each(function () {
                    var selectedtext = ($(this).parent().next().text());
                    if ($(this).is(':checked')) {
                        $("#Default_Currency").append('<option value="' + selectedtext + '">' + selectedtext + '</option>');
                    } else {
                        $('option[value*="' + selectedtext + '"]').remove();
                    }
                }); 

            });

        $(".ckbox").change(function (e) {
            var selectedtext = ($(this).parent().next().text());
                 if ($(this).is(':checked')) {
                    $("#Default_Currency").append('<option value="' + selectedtext + '">' + selectedtext + '</option>');
                } else {
                    $('option[value*="' + selectedtext + '"]').remove();
                }
            });
    </script>
}

Result:结果:

在此处输入图片说明

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

相关问题 在jQuery中选中复选框时,如何将项添加到列表中? - How can I add items to a list when a checkbox is checked in jQuery? 选中复选框时如何使列表项的文本变灰? - How to gray out the text of the list items when their checkbox is checked? 选中复选框时禁用下拉列表? - Disable dropdown list when the checkbox is checked? WordPress /重力表单-如何在单个表单中显示/隐藏项目 <ul> 列表基于被选中的复选框? - Wordpress/Gravity Forms - How to show/hide items in a single <ul> list based on a checkbox being checked? 如果选中了checkboxlist1中的特定列表项,如何禁用checkboxlist2中的某些复选框? - How to disable certain checkbox items in checkboxlist2 if a specific list item from checkboxlist1 is checked? 如何将项目添加到文本框提供的下拉列表中? - How to add items to a dropdown list provided from textbox? 如何在jQuery中添加/删除下拉多选列表项 - How to add/remove dropdown multiselect list items in jquery 我想从在 mat 复选框中检查的项目中列出一个列表 - I want to make a list from the items that are checked in mat checkbox 如果选中复选框,如何向 matInput 添加所需的验证 - How to add required validation to matInput if the checkbox is checked 如何添加显示选中的复选框的总值 - How to add a display a total value of the checkbox that are checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM