Disable drop down box on radio button click

2019-06-21 05:03发布

I have two radio buttons and a drop down box as you can see below. What I want to do is: 1. While no is checked, either hide, or grey out the drop down box, and 2. While yes is checked, show 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>

6条回答
forever°为你锁心
2楼-- · 2019-06-21 05:21

You need to set your select control to disabled and use a similar code to this one:

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

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

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

查看更多
甜甜的少女心
3楼-- · 2019-06-21 05:29
​$(function(){
  $('input:radio').bind('change', function(){
     $('#discountselection').attr('disabled', !$("#yes").is(":checked"));
  });
});​
查看更多
孤傲高冷的网名
4楼-- · 2019-06-21 05:31
   <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 dropdown's display style, or disabled property in HTML based on your default radiobutton selected on page load.

 <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>
查看更多
看我几分像从前
5楼-- · 2019-06-21 05:36

Would this do it?

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

Tested, works great. Good luck.

查看更多
我只想做你的唯一
6楼-- · 2019-06-21 05:44
$('input:radio[name="discount"]').change(function() {
    if ($(this).val()=='Yes') {
        $('#discountselection').attr('disabled',true);
    } else
        $('#discountselection').removeAttr('disabled');
});​

http://jsfiddle.net/uSmVD/

查看更多
手持菜刀,她持情操
7楼-- · 2019-06-21 05:44

You can hide it with jQuery:

$(document).ready(function(){     

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

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

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

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

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

查看更多
登录 后发表回答