繁体   English   中英

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

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

我正在开展一个项目,我必须切换<div>的可见性。

我有以下代码:

<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>

我想谈谈业务领域div。 因此,如果没有选择单选按钮或“个人”单选按钮:应隐藏div。 如果选择了“商家”单选按钮,我希望它显示。

目前,我正在使用此代码:

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

但是,我想知道我是否可以使用.toggle()函数执行此操作。

我建议使用的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> 

顺便提一下,请注意我还在<label>元素中包装了相关文本,以指示单选按钮的用途,以直接将该文本与<input>相关联,因此单击文本会自动检查<input>

参考文献:

如果可能的话,我通常倾向于不使用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> 

~代表任何兄弟姐妹,在我们在~符号之前定义的元素之后。

JS小提琴

试试这个吧

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

你可以这样使用:

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

.show和.hide很慢。 https://twitter.com/paul_irish/status/564443848613847040

最好使用javascript打开和关闭css类。 将类的css设置为{visibility:hidden}或{display:none}

使用以下代码

<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>

和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>

可能是一个更优雅的解决方案,在我看来它更具可读性,并且正如@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