2014-12-18 12 views
10

Pracuję nad projektem, w którym muszę przełączać widoczność <div>.Jak przełączyć widoczność div za pomocą przycisków radiowych?

Mam następujący kod:

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

chciałbym togle Business-Fields div. Tak więc, jeśli żaden z przycisków radiowych lub "osobisty" przycisk radiowy jest wybrany: Element div powinien być ukryty. Jeśli przycisk opcji "biznes" jest zaznaczony, chcę go pokazać.

Obecnie używam tego kodu:

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

Jednak, zastanawiałem się, czy mogę to zrobić za pomocą funkcji .toggle().

+4

dlaczego nie wystarczy użyć '.toggle()' wtedy zamiast show() i hide()? – WeSt

+2

@WeSt To działałoby tylko wtedy, gdy OP zmodyfikował kod, aby użyć zdarzenia 'change'. –

+0

dla mnie .. show() hide() jest najlepszym sposobem, a następnie .toggle(), ponieważ po ponownym wyborze osobistych pokazuje skrzynek .. – Sarath

Odpowiedz

12

Sugeruję użyciu zdarzenie change i dostarczanie logiczny przełącznik sposobu toggle(), która pokaże kolekcję jQuery elementów jeśli przełącznik ocenia się true i ukryć je, jeśli ocenia się 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>

Nawiasem mówiąc, owinięto również powiązany tekst, aby wskazać cel przycisku radiowego, wewnątrz elementu <label>, aby bezpośrednio skojarzyć ten tekst z <input>, więc kliknięcie tego tekstu automatycznie sprawdzi, czy kod <input> jest automatyczny.

Odniesienia:

+0

Jeden więcej rzeczy ... Jeśli chciałbym początkowo sprawdzić wartość i pokazać div. Jak bym to zrobił? – Peter

+0

Dodaj atrybut 'checked' do odpowiedniego pola wyboru; albo w HTML (zalecane) lub ustaw właściwość 'checked' na 'true', z jQuery lub JavaScript (' $ (' input [type = radio] [name = type] [value = 2] '). prop (' zaznaczone ', true); 'lub' document.querySelector (' input [type = radio] [name = type] [value = 2] ') checked = true; ') –

+0

@DavidThomas Rzeczywiście, już to naprawiłem w moim html , ale to nie powoduje przełączenia. – Peter

1

można używać tak:

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

Sprawdź to działa skrzypce: http://jsfiddle.net/65bvecsj/ –

1

użycie poniższy kod

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

i HTML jest-

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

JS Fiddle

Spróbuj jeden

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

przełączanie nie jest dobrą opcją: http://jsfiddle.net/jhLwa1nt/3/ najpierw sprawdź osobisty, wiesz, że wynik nie jest tym, co chciał OP –

8

Zazwyczaj nie używam JS jeśli to możliwe, dlatego przychodzi podejście oparte na HTML + CSS.

.bussines-type .business-fields { 
 
\t display: none; 
 
} 
 

 
.bussines-type input[value="2"]:checked ~ .business-fields { 
 
\t display: block; 
 
}
<div class="bussines-type"> 
 
\t <input id="bt1" type="radio" name="type" value="1"> 
 
     <label for="bt1"> Personal</label> 
 
\t <input id="bt2" type="radio" name="type" value="2"> 
 
     <label for="bt2"> Business</label> 
 

 
\t <div class="business-fields"> 
 
\t \t <input type="text" placeholder="Company name" name="company-name"> 
 
\t \t <input type="text" placeholder="Vat number" name="vat-number"> 
 
\t </div> 
 
</div>

W ~ oznacza dowolną rodzeństwa, które po elemencie zdefiniowanej przed znakiem ~.

1

Prawdopodobnie bardziej eleganckie rozwiązanie, Jest to nieco bardziej czytelne według mojej opinii, a jako @Ollie_W wskazuje, że może być bardziej wydajne niż przełącz (pokaż/ukryj).

$('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>

Powiązane problemy