I have a javascript query on an Edit.cshtml page that needs to input a value in a field based on the value of the dropdown.
I have checked the Ids of each field and they are correct. Based on the value of field VoteType (id is VoteType and value can either be the strings “Simple Majority” or “2/3 Majority”), I want to place a numeric value in the field Votes (id is Votes). If the DDL VoteType says “Simple Majority”, I want the numeric value from the field SimpleMajority (id is SimpleMajority) to go into the Votes field, otherwise I want to put the numeric value from the TwoThirdsMajority (id is TwoThirdsMajority) field in the Votes field. So, in the above image example: Because the DDL says “2/3 Majority”, the Votes field should say 95. If the DDL said “Simple Majority”, Votes would say 96.
Here is the Javascript that I currently have:
<script type="text/javascript">
function assignVoteTypeData() {
const votetype = [$("#VoteType").val()];
$("#Votes").val(votetype).trigger('input');
}
function updateVoteTypeData() {
if (voteType = "Simple Majority")
{
const votes = parseFloat($('#SimpleMajority').val());
}
else {
const votes = parseFloat($('#TwoThirdsMajority').val());
}
}
$(document).ready(function () {
$('#Votes').on('input', updateVoteTypeData);
});
</script>
Thought I was beginning to understand this stuff, but I just don’t have it correct yet. It doesn’t error out (no errors on F12 debug), but it doesn’t do anything either!
An extra set of eyes (and a better brain!) would be much appreciated. Thank you.
Snippets from Edit.cshtml if needed:
<td style="width: 25%">
<div class="mb-3">
<label asp-for="Vote.VoteType"></label>
<select asp-for="Vote.VoteType" id="VoteType" class="form-select" onchange="assignVoteTypeData()">
<option value="">---Select Vote Type---</option>
@if (Model.DisplayVoteTypeData != null)
{
@foreach (var item1 in Model.DisplayVoteTypeData.OrderBy(x => x.VoteTypeDesc))
{
<option value="@item1.VoteTypeDesc" selected="@(item1.VoteTypeDesc.ToString()==Model.Vote.VoteType?true:false)">@item1.VoteTypeDesc</option>
}
}
</select>
</div>
</td>
<div class="mb-3">
@Html.DisplayFor(modelItem => nameGroup.SimpleMajority, new { @id = "SimpleMajority" })
</div>
</td>
<td>
<div class="mb-3">
@Html.DisplayFor(modelItem => nameGroup.TwoThirdsMajority, new { @id = "TwoThirdsMajority" })
</div>
</td>
<td>
<div class="mb-3">
<input asp-for="Vote.Votes" id="Votes" type="number" class="form-control" readonly/>
</div>
</td>