Q:

How do I get the checkbox value if it is checked?

0

This code will help you to understand how to get the checkbox value if it is checked.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

To get the checkbox value when it is checked or unchecked use the onchange event by calling a method in onchange event using lambda expression and passing the checkbox value to it.

@foreach (var check in CheckBoxList())
{
 <input class="custom-checkbox" type="checkbox" @onchange="eventArgs => { CheckboxClicked(check, eventArgs.Value); }" />
@check
 <br />
}

@code {

public List<string> CheckBox { get; set; } = new List<string>();
void CheckboxClicked(string CheckID, object checkedValue)
 {
if ((bool)checkedValue)
{
        if (!CheckBox.Contains(CheckID))
        {
                CheckBox.Add(CheckID);
         }
 }
 else
 {
        if (CheckBox.Contains(CheckID))
         {
                CheckBox.Remove(CheckID);
         }
 }
 }

public List<String> CheckBoxList()
{
List<String> checkBox = new List<String>();
checkBox.Add("CheckBox 1");
checkBox.Add("CheckBox 2");
return checkBox;
}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now