Anees Jafrey
Anees Jafrey

Reputation: 31

How to write jQuery to disable dropdown on selecting an item from radiobutton list

I have this code but it doesn't work with asp.net page which has a master page along with ajax control toolkit(I have included ToolScriptManager). The code is

<script type="text/javascript">
    jQuery(function() {


        $("#<%=RadioButtonList1.ClientID%>").change(function() {
            var rbvalue = $("input[@name=<%=RadioButtonList1.UniqueID%>]:radio:checked").val();
            if (rbvalue == "No") {
                $("#DropDownList1").attr("disabled", false);
            }
            else if (rbvalue == "Yes") {
                $("#DropDownList1").attr("disabled", true);
            }
        });
    });
</script>

Upvotes: 3

Views: 3281

Answers (2)

K.Revathi
K.Revathi

Reputation: 11

<script type="text/javascript">
    $(document).ready(function() {

        $('#RadioButtonList1').change(function() {
            var rbvalue = $('#RadioButtonList1').val();
            if (rbvalue == "No") {
                $('#DropDownList1').attr('disabled', false);
            }
            else if (rbvalue == "Yes") {
                $('#DropDownList1').attr('disabled', true);
            }
        });
    });
</script>

Upvotes: 1

Michael Cox
Michael Cox

Reputation: 1301

If it works fine without the Master Page, you have to look at what that might change. The easiest answer is that it introduces a javascript bug that causes the rest of the code not to execute. I'd try a few things:

  1. View the source of this code both when working and not. Just to confirm that the .NET code is properly replacing the RadioButton ID values.
  2. Assuming the code is the same, load the page with the Master Page (non-working version) and open either Firebug in FireFox or the Chrome debugger. Make sure there are no javascript errors on page load. In fact, this is also a good way to confirm the code is running as expected because you can put in javascript break points.
  3. Or the simple way - add an alert('here!'); within the change function (above "var rbvalue = ..."). That'll confirm whether it's running or not, which will help you determine if the bug is caused by that function not running, or running incorrectly.

Upvotes: 0

Related Questions