简体   繁体   English

单击单选按钮时禁用下拉框

[英]Disable drop down box on radio button click

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. 我想要做的是:1。当没有选中时,隐藏或灰显下拉框,然后2.选中是,显示下拉框。

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>
   <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. 此外,根据页面加载时选择的默认单选按钮,在HTML中设置下拉列表的显示样式或禁用属性。

 <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>
$('input:radio[name="discount"]').change(function() {
    if ($(this).val()=='Yes') {
        $('#discountselection').attr('disabled',true);
    } else
        $('#discountselection').removeAttr('disabled');
});​

http://jsfiddle.net/uSmVD/ http://jsfiddle.net/uSmVD/

You need to set your select control to disabled and use a similar code to this one: 您需要将select控件设置为disabled并使用与此类似的代码:

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

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

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

You can hide it with jQuery: 你可以用jQuery隐藏它:

$(document).ready(function(){     

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

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

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

check it: http://www.jsfiddle.net/cFUsU/ 检查一下: http//www.jsfiddle.net/cFUsU/

UPDATE: added $(document).ready(); 更新:添加$(document).ready(); method to start set this code to action when the page is ready 在页面准备好时开始将此代码设置为操作的方法

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

Would this do it? 这样做吗?

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

Tested, works great. 经过测试,效果很好。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM