简体   繁体   中英

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. With one parameter it'll read the passed param, with two it'll set it.

Like $(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
            }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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