简体   繁体   English

选择某些单选按钮时切换显示并启用文本文件

[英]Toggle Show and Enable Textfiled When Certain Radio Button is Selected

I would like to be able to have an input field (initially hidden and disabled) to be shown and enabled every time the "Other" radio button is selected, and ofcoure to be hidden and disabled when a different radio button is selected . 我希望能够在每次选择“其他”单选按钮时显示并启用一个输入字段(最初是隐藏和禁用的),并且在选择其他单选按钮时也将隐藏并禁用一个输入字段。 I have gotten this script to show and hide the textfield appropriately, but Id like to know how I can get enabling and disabling to happen with it 我已经获得了该脚本来适当地显示和隐藏文本字段,但是我想知道如何才能启用和禁用它

$('input[name="x_description"]').bind('change',function(){
    var showOrHide = ($(this).val() == "Other") ? true : false;
    $('#other_program_text').toggle(showOrHide);  
});

I know that this is how to show the field and enable the textfield, but cant figure out how to put it all together, 我知道这是显示字段并启用文本字段的方法,但无法弄清楚如何将它们组合在一起,

$(this).show().prop('disabled', false);

Thanks in advance, Dan 预先感谢,丹

$('input[name="x_description"]').bind('change',function(){
    if ( showOrHide == true ) {
        $('#other_program_text').show().prop('disabled',false);
    } else if ( showOrHide == false ) {
        $('#other_program_text').hide().prop('disabled',true);
    }
});

You want to use the $(this).attr() function to set properties on an HTML element. 您想使用$(this).attr()函数在HTML元素上设置属性。 With one parameter it'll read the passed param, with two it'll set it. 使用一个参数将读取传递的参数,使用两个参数将其设置。

Like $(this).attr("enabled", true) 像$(this).attr(“ enabled”,true)

You can use "click" method 您可以使用“点击”方法

 $('input[name="x_description"]').click(function() {
       if($('#radio_button').is(':checked')) { 
           alert("it's checked"); //do your stuff
        }
 });

If your content are dynamically generated then use "live" method 如果您的内容是动态生成的,则使用“实时”方法

 $('input[name="x_description"]').live('click',function() {
           if($('#radio_button').is(':checked')) { 
               alert("it's checked"); //do your stuff
            }
 });

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

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