简体   繁体   English

在Jquery中切换detach()选择器

[英]Toggle detach() selector in Jquery

I have three radio buttons, I want to detach a div when input:radio[].val !== 'samplename' . 我有三个单选按钮,我想在input:radio[].val !== 'samplename'时分离div input:radio[].val !== 'samplename'

THESE ARE MY RADIO BUTTONS 这些是我的收音机按钮

<div class="form-group">
    <input checked="check" type="radio" value="Invididual" name="type_of_membership" id="radio-indi"> <label class="default-link" for="radio-indi">Individual</label>&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="radio" value="Institution" name="type_of_membership" id="radio-ins"> <label class="default-link" for="radio-ins">Institution</label>&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="radio" value="Corporation" name="type_of_membership" id="radio-corp"> <label class="default-link" for="radio-corp">Corporation</label>
</div>

THIS IS MY CURRENT JS SCRIPT 这是我当前的JS SCRIPT

var comapany_input = $("#company-div");

$("input[type='radio']").click(function() {

    if (!$('input:radio').is(':checked').val() == "Corporation") {
        comapany_input.detach();


    } else {

        comapany_input.appendTo("body");
        comapany_input = null;
    }
});

You need to use 你需要使用

if($('input:radio:checked').val() != "Corporation") {

to get the value of the checked checkbox. 获取已选中复选框的值。

if(!$('input:radio').is(':checked').val() == "Corporation") { will throw an error since the is method will return a boolean value which does not have the val method. if(!$('input:radio').is(':checked').val() == "Corporation") {将抛出一个错误,因为is方法将返回一个没有val方法的boolean值。

 var comapany_input = $("#company-div"); $("input[type='radio']").change(function() { if (this.checked && this.value == 'Corporation') { comapany_input.appendTo('body'); } else { comapany_input.detach(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group"> <input checked="check" type="radio" value="Invididual" name="type_of_membership" id="radio-indi"> <label class="default-link" for="radio-indi">Individual</label>&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio" value="Institution" name="type_of_membership" id="radio-ins"> <label class="default-link" for="radio-ins">Institution</label>&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio" value="Corporation" name="type_of_membership" id="radio-corp"> <label class="default-link" for="radio-corp">Corporation</label> </div> <div id="company-div">company-div</div> 


You could just hide/show the element instead like 你可以隐藏/显示元素而不是

 var comapany_input = $("#company-div"); $("input[type='radio']").change(function() { comapany_input.toggle(this.value == 'Corporation') }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group"> <input checked="check" type="radio" value="Invididual" name="type_of_membership" id="radio-indi"> <label class="default-link" for="radio-indi">Individual</label>&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio" value="Institution" name="type_of_membership" id="radio-ins"> <label class="default-link" for="radio-ins">Institution</label>&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio" value="Corporation" name="type_of_membership" id="radio-corp"> <label class="default-link" for="radio-corp">Corporation</label> </div> <div id="company-div">company-div</div> 

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

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