admin管理员组

文章数量:1323323

I have a RadioButtonList control in a web user control and it is loaded in a place holder of web page. The radio button list items will be loaded dynamically and the user can select any item.

As per the radio button behavior, we want to select another item to deselect already selected item. But I want to deselect an item if the user clicked on same item itself if it was already checked.

Also I have to do it using javascript not Server side code.

I have a RadioButtonList control in a web user control and it is loaded in a place holder of web page. The radio button list items will be loaded dynamically and the user can select any item.

As per the radio button behavior, we want to select another item to deselect already selected item. But I want to deselect an item if the user clicked on same item itself if it was already checked.

Also I have to do it using javascript not Server side code.

Share asked Dec 13, 2012 at 13:21 JobiJobi 1,1225 gold badges26 silver badges38 bronze badges 1
  • You've received some great answers in the past. How about you mark some of them as answered? It'll help you get better answers in the future. – Niklas Commented Dec 13, 2012 at 13:23
Add a ment  | 

2 Answers 2

Reset to default 5

Add this javascript function in <Head> tag.

<script type="text/javascript">


        function clearRadioButtonList() {

            var elementRef = document.getElementById('<%= yourRadioButtonListID.ClientID %>');
            var inputElementArray = elementRef.getElementsByTagName('input');

            for (var i = 0; i < inputElementArray.length; i++) {
                var inputElement = inputElementArray[i];

                inputElement.checked = false;
            }
            return false;
        }

    </script>

When you dynamically add listitem, add onclick attribute to listitem:

myListItem.Attributes.Add("onclick", "clearRadioButtonList");

Don't forget to replace "yourRadioButtonListID" in javascript function. This will uncheck the clicked radio button if it is checked.

Thanks.

Much easier with jquery

<script type="text/javascript">
    $(document).ready(function () {
        $('#btnTest').click(function () {
            var radiolist = $('#RadioButtonList1').find('input:radio');
            radiolist.removeAttr('checked');
        });
    });

</script>

本文标签: aspnetUncheck dynamically radiobuttonlist control using javascriptStack Overflow