简体   繁体   中英

C# ASP.NET How to retrieve the number or className of EditorFor via JavaScript

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!

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM