0

so in my view I have this

                for (int i = 0; i < Model.Count; i++)
                {  
                   @Html.EditorFor(m => m[i].IsSingle)
                }

And depends on the number of Model.Count it will generate the Checkbox, sometimes it is more than 10 and sometimes it is 7 based on the List given from the Controller.

What I needed to do next is to check that if the user ticked one of the IsSingle, then another div will be displays.

            $(document).ready(function () {


                if (document.getElementById("Single").checked == true) {
                    $("#tdSingle").show();
                }
                else {
                    $("#tdSingle").hide();

                }

                });
            });

I need to tweak the above code so that It will display the #tdSingle, if any of the checkbox is ticked. Anyone know how to achieve that?

This is the sample of the EditorFor code being generated

<input class="check-box" id="z0__IsSingle" name="[0].IsSingle" type="checkbox" value="true">

Sorry if I am being unclear.

Thanks a lot!

Sarah
  • 329
  • 5
  • 21
  • you can use $.change event handler (https://stackoverflow.com/questions/8423217/jquery-checkbox-checked-state-changed-event) with the selector provided by Ricardo – Robin B Apr 13 '18 at 11:46
  • Possible duplicate of [Wildcards in jQuery selectors](https://stackoverflow.com/questions/5376431/wildcards-in-jquery-selectors) – freedomn-m Apr 13 '18 at 11:51

1 Answers1

1

You can use a JQuery selector to verify if any checkbox tht has "IsSingle" in name is checked:

var isSingleChecked = $("input[name*='IsSingle']:checked").length > 0;

name*='IsSingle' will find any input where its name contains "IsSingle", and :checked will guarantee that is checked.

Reference to JQuery documentation:
https://api.jquery.com/attribute-contains-selector/
https://api.jquery.com/selected-selector/

Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43