简体   繁体   English

为选定的单选按钮div添加类别名称

[英]Add class name for selected radio button div

<div>
    One : <input type="radio" name="typeof" id="typeof1-grey" value="grey" />
    Two : <input type="radio" name="typeof" id="typeof2-pink" value="pink" />
    Three : <input type="radio" name="typeof" id="typeof3-green" value="green" />   
<div>
<div id="one" style="display:none;">
    ABC : <input type="text" name="abc" value="" />
    PQR : <input type="text" name="pqr" value="" />
</div>
<div id="two" style="display:none;">
    XYZ : <input type="text" name="xyz" value="" />
</div>
<div id="three" style="display:none;">
    Full Name: <input type="text" name="fname" value="" />
</div>

If select "One"(Grey) radio button, i need to show DIV id "one" and add class name(required) to all input fields with in the div. 如果选择“一个”(灰色)单选按钮,我需要显示DIV id“一个”,并将类名(必填)添加到div中的所有输入字段中。 The same way if i change to pink radio button remove the previous div class name and add into the DIV id two using Jquery. 如果我更改为粉红色单选按钮,则以相同的方式删除先前的div类名称,并使用Jquery将其添加到DIV id中。

Please help me! 请帮我!

You can relate the radio controls to the div by their index . 您可以通过其index将单选控件与div相关联。 You can then add/remove the classes as needed. 然后,您可以根据需要添加/删除类。 Try this: 尝试这个:

$('input[type="radio"]').change(function() {
    $('input[type="text"]').removeClass('required');
    $('div').not(':first').hide();
    $('div').eq($(this).index() + 1).show().find('input').addClass('required');
});

Note that you can simplify this logic by the use of classes in your HTML: 请注意,您可以通过在HTML中使用类来简化此逻辑:

<div>
    One : <input type="radio" name="typeof" id="typeof1-grey" class="radio" value="grey" />
    Two : <input type="radio" name="typeof" id="typeof2-pink" class="radio" value="pink" />
    Three : <input type="radio" name="typeof" id="typeof3-green" class="radio" value="green" />   
<div>
<div id="one" class="optional-div">
    ABC : <input type="text" name="abc" value="" class="text-input" />
    PQR : <input type="text" name="pqr" value="" class="text-input" />
</div>
<div id="two" class="optional-div">
    XYZ : <input type="text" name="xyz" value="" class="text-input" />
</div>
<div id="three" class="optional-div">
    Full Name: <input type="text" name="fname" value="" class="text-input" />
</div>
$('.radio').change(function() {
    $('.text-input').removeClass('required');
    $('.optional-div').hide().eq($(this).index()).show().find('.text-input').addClass('required');
});

Example fiddle 小提琴的例子

Here's my solution, I added a class sub to every div element which has to show/hide when a radiobutton is clicked. 这是我的解决方案,我向每个div元素添加了一个class子元素,当单击单选按钮时,该子元素必须显示/隐藏。

 var divs = ['#one', '#two', '#three']; $('input[type="radio"]').change(function() { var current = $(divs[$(this).index()]); $('.sub').hide(); $('input').removeClass('required'); current.show(); current.children('input').addClass('required'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> One : <input type="radio" name="typeof" id="typeof1-grey" value="grey" /> Two : <input type="radio" name="typeof" id="typeof2-pink" value="pink" /> Three : <input type="radio" name="typeof" id="typeof3-green" value="green" /> <div> <div id="one" class="sub" style="display:none;"> ABC : <input type="text" name="abc" value="" /> PQR : <input type="text" name="pqr" value="" /> </div> <div id="two" class="sub" style="display:none;"> XYZ : <input type="text" name="xyz" value="" /> </div> <div id="three" class="sub" style="display:none;"> Full Name: <input type="text" name="fname" value="" /> </div> 

The only thing that you have to do is add the classname to the divs array when you make a fourth option. 唯一要做的就是在第四个选项中将类名添加到divs数组中。 Goodluck. 祝好运。

Is that what you are looking for ?? 那是您要找的东西吗?

html html

<div>
    One : <input type="radio" name="typeof" data-target="#one" class="radiobtn" value="grey" />
    Two : <input type="radio" name="typeof" data-target="#two" class="radiobtn" value="pink" />
    Three : <input type="radio" name="typeof" data-target="#three" class="radiobtn" value="green" />   
<div>
<div class="toTarget" id="one" >
    ABC : <input type="text" name="abc" value="" />
    PQR : <input type="text" name="pqr" value="" />
</div>
<div class="toTarget" id="two" >
    XYZ : <input type="text" name="xyz" value="" />
</div>
<div class="toTarget" id="three" >
    Full Name: <input type="text" name="fname" value="" />
</div>

CSS 的CSS

.toTarget{
  display:none;
}

javascript javascript

$('.radiobtn').click(function(){
var color = $(this).val();
var Thistarget = $(this).attr('data-target');
$('.toTarget').css('display','none');
$(Thistarget+' input').attr('required');
$(Thistarget).css({
'display':'block'
});
});

Working Fiddle 工作小提琴

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

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