简体   繁体   English

如何使用单选按钮切换div可见性?

[英]How to toggle div visibility using radio buttons?

I'm working on a project in which I have to toggle the visibility of a <div> . 我正在开展一个项目,我必须切换<div>的可见性。

I've got the following code: 我有以下代码:

<input type="radio" name="type" value="1"> Personal
<input type="radio" name="type" value="2"> Business

<div class="business-fields">
    <input type="text" name="company-name">
    <input type="text" name="vat-number">
</div>

I would like to togle the business-fields div. 我想谈谈业务领域div。 So, if none of the radio buttons, or the 'personal' radio button is selected: The div should be hidden. 因此,如果没有选择单选按钮或“个人”单选按钮:应隐藏div。 If the 'business' radio button is selected, I want it to show. 如果选择了“商家”单选按钮,我希望它显示。

Currently, I am using this code: 目前,我正在使用此代码:

$("input[name='type']").click(function() {
    var status = $(this).val();
    if (status == 2) {
        $(".business-fields").show();
    } else {
        $(".business-fields").hide();
    }
});

However, I was wondering if I can do this using the .toggle() function. 但是,我想知道我是否可以使用.toggle()函数执行此操作。

I'd suggest using the change event, and supplying a Boolean switch to the toggle() method, which will show the jQuery collection of elements if the switch evaluates to true , and hide them if it evaluates to false : 我建议使用的change情况,以及供应布尔开关toggle()方法,它会显示元素的jQuery的集合如果交换机的计算结果为true ,并隐藏他们,如果计算结果为false

 // select the relevant <input> elements, and using on() to bind a change event-handler: $('input[name="type"]').on('change', function() { // this, in the anonymous function, refers to the changed-<input>: // select the element(s) you want to show/hide: $('.business-fields') // pass a Boolean to the method, if the numeric-value of the changed-<input> // is exactly equal to 2 and that <input> is checked, the .business-fields // will be shown: .toggle(+this.value === 2 && this.checked); // trigger the change event, to show/hide the .business-fields element(s) on // page-load: }).change(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label> <input type="radio" name="type" value="1">Personal</label> <label> <input type="radio" name="type" value="2">Business</label> <div class="business-fields"> <input type="text" name="company-name"> <input type="text" name="vat-number"> </div> 

Incidentally, note I've also wrapped the associated text, to indicate the radio-button's purpose, inside of a <label> element to directly associate that text with the <input> , so clicking the text checks the <input> automatically. 顺便提一下,请注意我还在<label>元素中包装了相关文本,以指示单选按钮的用途,以直接将该文本与<input>相关联,因此单击文本会自动检查<input>

References: 参考文献:

I usually tend not to use JS if possible, therefore here comes a HTML+CSS way approach. 如果可能的话,我通常倾向于不使用JS,因此这里提供了一种HTML + CSS方式。

 .bussines-type .business-fields { display: none; } .bussines-type input[value="2"]:checked ~ .business-fields { display: block; } 
 <div class="bussines-type"> <input id="bt1" type="radio" name="type" value="1"> <label for="bt1"> Personal</label> <input id="bt2" type="radio" name="type" value="2"> <label for="bt2"> Business</label> <div class="business-fields"> <input type="text" placeholder="Company name" name="company-name"> <input type="text" placeholder="Vat number" name="vat-number"> </div> </div> 

The ~ stands for any siblings, that are after the element we defined before the ~ sign. ~代表任何兄弟姐妹,在我们在~符号之前定义的元素之后。

JS Fiddle JS小提琴

Try this one 试试这个吧

<input type="radio" name="type" value="1" checked ="true"> Personal
<input type="radio" name="type" value="2"> Business

<div class="business-fields">
    <input type="text" name="company-name">
    <input type="text" name="vat-number">
</div>

.business-fields{
    display: none;
}



    $("input[name='type']").change(function() {
        $(".business-fields").toggle();
});

You may use like this: 你可以这样使用:

$("input[name='type']").change(function() {
    var status = $(this).val();
    if (status != 2) {
        $(".business-fields").hide();
    } else {
        $(".business-fields").show();
    }
});

.show and .hide are pretty slow. .show和.hide很慢。 https://twitter.com/paul_irish/status/564443848613847040 https://twitter.com/paul_irish/status/564443848613847040

It's better to toggle a css class on and off with javascript. 最好使用javascript打开和关闭css类。 Set the css of the class to {visibility: hidden} or {display: none} 将类的css设置为{visibility:hidden}或{display:none}

use the below code 使用以下代码

<script>
$(function(){
$(":radio[value=1]").click(function(){
    var isVisible = $( ".business-fields" ).is( ":visible" );
    if(isVisible==true)
        $('.business-fields').toggle();
});

$(":radio[value=2]").click(function(){
    var isVisible = $( ".business-fields" ).is( ":visible" );
    if(isVisible==false)
        $('.business-fields').toggle();
});
});
</script>

AND HTML is- 和HTML是 -

<input name="type" type="radio" value="1" >Personal
<input type="radio" name="type" value="2" checked="checked"> Business

<div class="business-fields">
    <input type="text" name="company-name">
    <input type="text" name="vat-number">
</div>

Possibly a more elegant solution, It's a bit more readable in my opinion, and and as @Ollie_W points out it might be more performant that toggle (show/hide). 可能是一个更优雅的解决方案,在我看来它更具可读性,并且正如@Ollie_W指出的那样切换(显示/隐藏)可能更具性能。

 $('input[name="type"]').on('change', function(event) { var radioButton = $(event.currentTarget), isBusiness = radioButton.val() === 'business' && radioButton.prop('checked'); $('.business-fields').toggleClass('hidden', !isBusiness); }).change(); 
 .hidden { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label> <input type="radio" name="type" value="personal">Personal</label> <label> <input type="radio" name="type" value="business">Business</label> <div class="business-fields hidden"> <input type="text" name="company-name"> <input type="text" name="vat-number"> </div> 

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

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