vendredi 5 mai 2023

jQuery Pull Label for HTML Checkbox into Comma-Separated String

I have an .aspx with multiple HTML checkboxes. I am trying to pull the value of all checked checkboxes into a comma-separated string. I can get the ID of the checkbox, but what I really want is the label text.

.aspx:

<div id="divIssueTypes">
    <label for="CDAccess">Access</label>
    <%: Html.CheckBox("CDAccess") %>
    
    <label for="CDReporting">Reporting</label>
    <%: Html.CheckBox("CDReporting") %>
    
    <label for="CDSeparatelyLocked">Separately locked (subject to abuse)</label>
    <%: Html.CheckBox("CDSeparatelyLocked") %>
    
    <label for="CDEmergencyAccess">Emergency access</label>
    <%: Html.CheckBox("CDEmergencyAccess") %>
    
    <label for="CDCounts">Counts</label>
    <%: Html.CheckBox("CDCounts") %>
    
    <label for="CDDisposition">Disposition</label>
    <%: Html.CheckBox("CDDisposition") %>
    
    <label for="CDDiversion">Diversion</label>
    <%= Html.CheckBox("CDDiversion") %>
</div>

jQuery:

var checkedVals = ($("#divIssueTypes").find('input:checkbox:checked').map( function() {
        return this.id;
    }).get().join(","));

If, for example, all checkboxes were checked my code would return: CDAccess,CDReporting,CDSeparatelyLocked,CDEmergencyAccess,CDCounts,CDDisposition,CDDiversion

But I would like it to return:
Access,Reporting,Separately locked (subject to abuse),Emergency access,Counts,Disposition,Diversion

Is this at all possible?




Aucun commentaire:

Enregistrer un commentaire