禁用单选按钮单击上的下拉框

发布于 2024-09-18 22:25:34 字数 610 浏览 4 评论 0原文

我有两个单选按钮和一个下拉框,如下所示。我想做的是: 1. 当选中“否”时,隐藏或灰显下拉框,然后 2. 当选中“是”时,显示下拉框。

任何指示将不胜感激!

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

じее 2024-09-25 22:25:35
   <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>

此外,还可以根据页面加载时选择的默认单选按钮设置下拉列表的显示样式或 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>
   <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>
夜血缘 2024-09-25 22:25:35
$('input:radio[name="discount"]').change(function() {
    if ($(this).val()=='Yes') {
        $('#discountselection').attr('disabled',true);
    } else
        $('#discountselection').removeAttr('disabled');
});​

http://jsfiddle.net/uSmVD/

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

http://jsfiddle.net/uSmVD/

小兔几 2024-09-25 22:25:35

您需要将 select 控件设置为 disabled 并使用与此类似的代码:

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

请参阅 http://www.jsfiddle.net/A3BuQ/6/

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

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()

生生漫 2024-09-25 22:25:35
​$(function(){
  $('input:radio').bind('change', function(){
     $('#discountselection').attr('disabled', !$("#yes").is(":checked"));
  });
});​
​$(function(){
  $('input:radio').bind('change', function(){
     $('#discountselection').attr('disabled', !$("#yes").is(":checked"));
  });
});​
你曾走过我的故事 2024-09-25 22:25:35

这能行吗?

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

经过测试,效果很好。祝你好运。

Would this do it?

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

Tested, works great. Good luck.

乄_柒ぐ汐 2024-09-25 22:25:35

你可以用 jQuery 隐藏它:

$(document).ready(function(){     

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

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

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

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

更新:添加 $(document).ready ();页面准备好时开始设置此代码以执行操作的方法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文