Disable the dropdown menu when you click on the switch

I have two radio buttons and a drop-down list, as you can see below. I want to do the following: 1. If none of them are installed, either hide or the gray-drop-down window, and 2. For now, check the drop-down box.

Any pointers would be appreciated!

<td colspan="4">
<input name="discount" type="radio" id="Yes" value="Yes" />Yes
<input name="discount" type="radio" id="No" value="No" checked="checked" />No<br />  
<select class="purple" name="discountselection" id="discountselection">
<option value="1 Year" selected="selected">1 Year</option>
<option value="2 Years">2 Years</option>
<option value="3 Years">3 Years</option>
</select>                  
</td>
+5
source share
6 answers
   <script type="text/javascript">
                   $("#Yes").click(function() {
                        $("#discountselection").attr("disabled", false);
                        //$("#discountselection").show(); //To Show the dropdown
                    });
                    $("#No").click(function() {
                        $("#discountselection").attr("disabled", true);
                        //$("#discountselection").hide();//To hide the dropdown
                    });
    </script>

Also, set the display style for the drop-down list or disable the property in HTML based on your standard radio selected when the page loads.

 <select  name="discountselection" id="discountselection" disabled="disabled">
    <option value="1 Year" selected="selected">1 Year</option>
    <option value="2 Years">2 Years</option>
    <option value="3 Years">3 Years</option>
    </select>
+18
source
$('input:radio[name="discount"]').change(function() {
    if ($(this).val()=='Yes') {
        $('#discountselection').attr('disabled',true);
    } else
        $('#discountselection').removeAttr('disabled');
});​

http://jsfiddle.net/uSmVD/

+3
source

select disabled :

​$(function(){
    $('input:radio[name=discount]').one('change', function(){
        $('.purple').removeAttr('disabled');
    });
});​

See http://www.jsfiddle.net/A3BuQ/6/

Ref . : . one () , . removeAttr ()

+2
source

You can hide it with jQuery:

$(document).ready(function(){     

$('#discountselection').hide();

        $('#No').click(function(){
            $('#discountselection').hide();
        });

        $('#Yes').click(function(){
            $('#discountselection').show();
        });
});

check: http://www.jsfiddle.net/cFUsU/

UPDATE: added $ (document) .ready (); method to start setting this code into action when the page is ready

+1
source
​$(function(){
  $('input:radio').bind('change', function(){
     $('#discountselection').attr('disabled', !$("#yes").is(":checked"));
  });
});​
+1
source

Will it do it?

$('input:radio').bind('change', function(){
    $('select').attr('disabled', $(this).val() == "No");
});

Tested, works great. Good luck.

+1
source

All Articles