选择单选按钮时隐藏/显示3个文本框

Hide/display of 3 textboxes on selection of radio button

本文关键字:3个 文本 显示 单选按钮 隐藏 选择      更新时间:2023-09-26

我有两个单选按钮。在选择一个时,我想显示3个文本框,并在选择另一个时隐藏它。

这是代码。

这是我的两个单选按钮。

<input type="radio" name="type"> Fresher
<input type="radio" name="type"> Experienced 

点击有经验的单选按钮后,我想显示这3个文本框。

Company Name: <input type="text"  hidden="true"/> <br/>
Designation: <input type="text" hidden="true"/> <br/>
Year_of_Experience: <input type="text"  hidden="true"/> <br/>

请帮我用javascript来解决这个问题。

您可以按如下方式执行此操作。首先将您的HTML更改为:

<input type="radio" name="type" value="Fresher"> Fresher
<input type="radio" name="type" value="Experienced"> Experienced
<div id="textboxes" style="display: none">
    Company Name: <input type="text" hidden="true"/> 
    Designation: <input type="text" hidden="true"/> 
    Year_of_Experience: <input type="text" hidden="true"/> 
</div>

此代码为代码添加的内容是为单选按钮设置一个值。这使我们能够根据选择的单选按钮进行选择。其次,在<div>中将输入字段分组在一起,以便于隐藏三个输入字段。修改HTML后,在您的网站上包含jQuery并使用以下代码:

$(function() {
    $('input[name="type"]').on('click', function() {
        if ($(this).val() == 'Experienced') {
            $('#textboxes').show();
        }
        else {
            $('#textboxes').hide();
        }
    });
});

您可以使用这个JSFiddle看到这是如何工作的:http://jsfiddle.net/pHsyj/

这是最好的例子。试试这样的东西。,这是的一个例子

$("input[type='radio']").change(function(){
if($(this).val()=="other")
{
    $("#otherAnswer").show();
}
else
{
       $("#otherAnswer").hide(); 
}
});

http://jsfiddle.net/Wc2GS/8/

我想这会解决你的问题

<input type="radio" name="type" id='frsradio'> Fresher
<input type="radio" name="type" id='expradio'> Experienced <br>
<input type="text" class='txbx' hidden="true"/> <br/>
 <input type="text" class='txbx' hidden="true"/> <br/>
<input type="text" class='txbx' hidden="true"/> <br/>
   $(function() {
        $('#expradio').click(function() {
            $('.txbx').attr('hidden',false);
        });           
        $('#frsradio').click(function() {
            $('.txbx').attr('hidden',true);
        });
    });

工作jsfiddle

请看,您的html元素没有唯一的标识,我只是以通用的方式给出了这段代码。

试试,

$('input[name="type"]:eq(1)').change(function(){
     $('input[type="text"]').toggle(this.checked);
});
<input type="radio" name="type" onClick ="radio"> Fresher
<input type="radio" name="type" onClick ="radio">  Experienced 

点击有经验的单选按钮,我想显示这3个文本框。

<input type="text"  hidden="true" class="text"/> <br/>
 <input type="text" hidden="true" class="text"/> <br/>
<input type="text"  hidden="true" class="text"/> <br/>

和javascript:-

<script>
 function radio()
{
     var result = document.getElementsByClassName('text'").style.display="none";
}
</script>

试试这个

<input type="radio" name="type" value="fresher"> Fresher
<input type="radio" name="type" value="exp"> Experienced 
<br/>
<input class="box" type="text"  hidden="true"/> <br/>
<input class="box" type="text" hidden="true"/> <br/>
<input class="box" type="text"  hidden="true"/> <br/>

编写脚本

$("input[type='radio']").on('change',function(){
if($(this).val() == "exp")
   $('.box').show('slow');
else
    $('.box').hide();
});

演示

试试这个。。。。

<input type="radio" name="type" id="fresh"> Fresher
<input type="radio" name="type" id="exp"> Experienced 
<input type="text" class="hid" hidden="true"/> <br/>
<input type="text" class="hid" hidden="true"/> <br/>
 <input type="text" class="hid"  hidden="true"/> <br/>
 $("#fresh").change(function(){
     $('.hid').css("style","display:none");  
 });
 $("#exp").change(function(){
     $('.hid').css("style","display:block");  
 });
For default hide the text boxes In OnRender Complete Method you need hide the three textbox for that you put the below code.
                    $("#textbox 1").hide();
                    $("#textbox 2").hide();
                    $("#textbox 3").hide();

你需要把代码写在零钱上的单选按钮上。

 Using the Id get the value of radio button
                    var val=$('#radioId').val;
                      if(val=='Fresher')
        {
                    $("#textbox 1").show();
                    $("#textbox 2").show();
                    $("#textbox 3").show();
                    }
                   else if (val=='Experienced'){
                    $("#textbox 1").hide();
                    $("#textbox 2").hide();
                    $("#textbox 3").hide();
                     }