2013-04-12 217 views
4

我想隱藏一個div,當有人單擊窗體上的單選按鈕(第二個單選按鈕)時,但當單擊第一個單選按鈕時,div顯示備份(切換)。這是我的工作代碼:http://jsfiddle.net/NKr7M/單擊單選按鈕,隱藏div

這裏的HTML,如果你想:

<div class="grid howAnswer"> 
    <div class="col-1"> 
     <div class="button-left"> 
      <img src="http://i.imgur.com/RVmh2rz.png" /> 

      <input checked="checked" type="radio" id="id_delivery_0" value="chat" name="delivery" onclick="toggle(this)" /> 
     </div><!-- .button-left --> 

     <div class="text-area top-text-area"> 
      <label for="id_delivery_0"> 
       AnswerChat By Appointment (Fastest) 
      </label> 
     </div><!-- .text-area --> 
    </div><!-- .col-1 --> 

    <div class="col-1"> 
     <div class="button-left" style="margin-left: 15px;"> 
      <img src="http://i.imgur.com/8J0SEVa.png" /> 

      <input type="radio" id="id_delivery_2" value="email" name="delivery" onclick="toggle(this)" /> 
     </div><!-- .button-left --> 

     <div class="text-area bottom-text-area"> 
      <label for="id_delivery_2"> 
       AnswerMail ASAP (Within 1-2 days) 
      </label> 
     </div><!-- .text-area --> 
    </div><!-- .col-1 --> 
</div><!-- .grid --> 

<!-- THIS IS WHAT I WANT TO HIDE --> 
<div class="formInput"> 
    <p>Let's try and hide this div</p> 
</div><!-- .formInput --> 

還有CSS:

/* General Declarations */ 
body { font-family: Arial, Helvetica, sans-serif; } 

ul li { list-style: none; } 

/* Form */ 
.col-1 li.howDoPadding { padding-bottom: 10px!important; } 

.byAppointment { margin: 0 0 -4px 10px;} 

.offlineForm .lightBlueText { 
    color: #80A9BD; 
    display: inline; 
} 

/* Grid */ 
*, *:after, *:before { 
    box-sizing: border-box; 
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
} 

.grid:after { 
    clear: both; 
    content: ""; 
    display: table; 
} 

.grid.howAnswer > div.col-1 { 
    padding-left: 90px; 
    position: relative; 
} 

.grid.howAnswer .button-left { 
    width: 80px; 
    position: absolute; 
    margin-left: 20px; 
    left: 0; 
    top: 0; 
} 

.button-left img { margin-right: -5px; } 

.top-text-area { margin: 15px 0 10px; } 

.bottom-text-area { margin: 10px 0 15px; } 

.button-left { margin: 10px 0; } 

/* Grid Gutters */ 
[class*='col-'] { 
    float: left; 
    padding-right: 20px; 
} 

[class*='col-']:last-of-type { padding-right: 0; } 

.col-1 { width: 100%; } 

我不是很精通JavaScript的,所以我會非常感謝我能得到的任何幫助。謝謝!

+4

哪裏是你試圖在小提琴的JS? – Huangism

+0

我還沒有嘗試任何JavaScript解決方案,這就是爲什麼我沒有包含任何 –

回答

5

LIVE DEMO

$(':radio[name=delivery]').change(function(){ // Or simply: $("[name=delivery]") 
    $('.formInput').toggle(); 
}); 
+2

+1確實美麗! –

+0

這是什麼選擇器語法:radio [name = delivery]我從來沒有見過它。 我已經準確地將你在這裏寫下的小提琴和選擇器的區別。 –

+0

@DylanHayes你不需要標籤選擇器,但是如果你有更多的'name = delivery'元素,它只是更具體的http://jsfiddle.net/NKr7M/7/。而不是'.formInput',我會去找一個ID。 –

1

您可以使用toggle()做到這一點:

$('#id_delivery_0').on("click", function(){ 
    $(".formInput").hide(); 
}); 
$('#id_delivery_2').on("click", function(){ 
    $(".formInput").show(); 
}); 

工作小提琴:

http://jsfiddle.net/NKr7M/4/

+1

@CyrilPangilinan我意識到這一點,但我確定他可以修改代碼以使用單個按鈕。 –

+1

@everyone :)更新....因爲有OP爲自己嘗試的東西顯然是皺起了眉頭 –

+1

哈哈哈我愛當人們刪除他們的評論,然後你留下來自己說話!無價! –

2

試試這個:

$("#id_delivery_0").on("change",function(){ 
    if($(this).prop("checked")) 
     $(".formInput").hide();  
}); 

$("#id_delivery_2").on("change",function(){ 
    if($(this).prop("checked")) 
     $(".formInput").show();  
}); 

的jsfiddlehttp://jsfiddle.net/hescano/NKr7M/3/

0

我假設jQuery庫是好的,因爲你這個標記與jQuery,所以這裏的jQuery的響應:

$(document).ready(function() { 
    $("#id_delivery_0").click(function() { 
     $(".formInput").show(); 
    }); 
    $("#id_delivery_2").click(function() { 
     $(".formInput").hide(); 
    }); 
}); 

一定要包括jQuery庫也是如此。

0
$("#id_delivery_0").on('click',function(){ 
     $(".formInput").find('p').show(); 
}); 

$("#id_delivery_2").on('click',function(){ 
     $(".formInput").find('p').hide(); 
}); 

Fiddle

0
$(document).ready(function(){ 

    $('#button1').click(function(event) { 
    $('#hide').hide(); 
}); 

    $('#button2').click(function(event) { 
    $('#hide').show(); 
    }); 
}); 
+1

你應該解釋你的答案 – Jens

+0

當單選按鈕1單擊時,演示jQuery hide()方法,隱藏id =「hide」elements.and當單選按鈕2單擊時,jQuery show()方法可見id =「hide」 textfiled,textarea等。 –

相關問題