One approach to solving this would be to store the Capacity
value in a data
attribute on the option
element so that it can be read on the client side. This would be generated along with the HTML so the extra JS object generation and lookup wouldn't be necessary.
The only changes you'd need to make is to add the Capacity
field to the model in the ListOfOrganisations
and then generate the select
HTML manually, instead of using asp-items
. It would look something like this:
public class OrganisationItem {
public string Text { get; set; }
public string Value { get; set; }
public string Capacity { get; set; }
}
<label>Organization</label>
<div>
<select name="Organization" class="form-control" id="SelectedOrg">
@foreach(var item in ViewBag.ListofOrganizations) {
<option value="@item.Value" data-capacity="@item.Capacity">@item.Text</option>
}
</select>
<div id="result"></div>
</div>
$('#SelectedOrg').on('change', e => {
let capacity = $(el.target).children('option:selected').data('capacity');
$('#result').text(capacity);
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…