locked
Disabling the options in a checkboxI list with Jquery RRS feed

  • Question

  • I have a Web Part which has 35+ Questions and answers. 

    Each of the answers are displayed in different controls. 

    1. Checkboxlist, dropdownlist etc...

    I am trying to write a jquery which will disable the other option in the checkboxlist when one of them is selected( a particular one) else user can select multiple answers. 

    Here is my code. 

    I haven't done anything in the server side 

    <script>
    
        $('#<%=lstValue.ClientID%> input').on('click', function (evt) {
        var clickedEl = evt.target;
        if (clickedEl.checked) {
            $('#<%=lstValue.ClientID%> input').each(function () {
                $(this).attr('disabled', true);
            });
            $(clickedEl).attr('disabled', false);
        } else {
            $('#<%=lstValue.ClientID%> input').each(function () {
                $(this).attr('disabled', false);
            });
        }
       });
      </script>
     

    <h3> One</h3> <asp:CheckBoxList ID="lstValue" runat="server"> <asp:ListItem>Short</asp:ListItem> <asp:ListItem>Medium</asp:ListItem>

      <asp:ListItem>Long</asp:ListItem>

    <asp:ListItem >Unknown</asp:ListItem> </asp:CheckBoxList>


    Any ideas how this code can work


    kukdai



    • Edited by kukdai Wednesday, February 12, 2014 7:59 PM name corrections
    • Moved by Caillen Thursday, February 13, 2014 2:07 AM
    Wednesday, February 12, 2014 7:57 PM

Answers

  • This is the answer

        $(document).ready(function () {
            var checked = false;
            $('#<%=lstExposureValue.ClientID%> input:checkbox').click(function () {
                var currentIdone = 'Unknown';
                var currentId = $(this).next().html();
                if (currentId == currentIdone) {

                    if (checked) {

                        $("#<%=lstExposureValue.ClientID%> input").removeAttr('disabled');
                        checked = false;
                        return;
                    }
                    else {
                        $("#<%=lstExposureValue.ClientID%> input").attr('checked', false);
                        $(this).attr('checked', true);
                        $('#<%=lstExposureValue.ClientID%> input:not(:checked)').attr('disabled', 'disabled');
                        checked = true;
                    }


                }

            });
        });


    kukdai

    • Marked as answer by kukdai Thursday, February 13, 2014 6:58 PM
    Thursday, February 13, 2014 6:58 PM

All replies

  • Hi kukdai,

    Please post your question in ASP.NET forum where you could get better responses.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 13, 2014 2:06 AM
  • This is the answer

        $(document).ready(function () {
            var checked = false;
            $('#<%=lstExposureValue.ClientID%> input:checkbox').click(function () {
                var currentIdone = 'Unknown';
                var currentId = $(this).next().html();
                if (currentId == currentIdone) {

                    if (checked) {

                        $("#<%=lstExposureValue.ClientID%> input").removeAttr('disabled');
                        checked = false;
                        return;
                    }
                    else {
                        $("#<%=lstExposureValue.ClientID%> input").attr('checked', false);
                        $(this).attr('checked', true);
                        $('#<%=lstExposureValue.ClientID%> input:not(:checked)').attr('disabled', 'disabled');
                        checked = true;
                    }


                }

            });
        });


    kukdai

    • Marked as answer by kukdai Thursday, February 13, 2014 6:58 PM
    Thursday, February 13, 2014 6:58 PM