[英]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.