2012-09-04 149 views
0

我正在嘗試做一個簡單的隱藏/顯示效果,具體取決於選中的單選按鈕。 它不能正常工作,我不知道爲什麼。 當用戶點擊「Oui」時,我想隱藏表格(#reprint_oui)到SHOW ...,如果選擇「Non」,則隱藏。單選按鈕選擇 - 隱藏並顯示字段

這裏是我的JS代碼:

$(document).ready(function(){ 

$("input[name$='reprint_group']").click(function(){ 

var radio_value = $(this).val(); 

if(radio_value=='Oui') { 
    $("#reprint_oui").show("slow"); 
} 
else if(radio_value=='Non') { 
    $("#reprint_oui").show("slow"); 
    } 
    }); 

$("#reprint_oui").hide(); 

});​ 

這裏是我的HTML:

<h2>Fiche technique de production</h2> 
     <table width="100%" border="0"> 
      <tr> 
      <td width="80%">Avons-nous par le passé produit des affiches que vous vous apprêtez à commander?</td> 
      <td width="20%"><label for="oui_reprint">Oui</label> 
      <input name="reprint_group" type="radio" id="oui_reprint" value="Oui"> 
      <label for="non_newprint">Non</label> 
      <input type="radio" name="reprint_group" id="non_newprint" value="Non"></td> 
     </tr> 
     </table> 
     <table width="100%" border="0" id="reprint_oui"> 
     <tr> 
      <td width="80%">S'agit-il d'une réimpression sans changement?</td> 
      <td width="20%"><label for="oui_reprint">Oui</label> 
      <input type="radio" name="reprint_sans_changement" id="oui_reprint" value="Oui"> 
      <label for="non_newprint">Non</label> 
      <input type="radio" name="reprint_sans_changement" id="non_newprint" value="Non"></td> 
     </tr> 
     </table> 

這裏是我的fiddle

謝謝

+0

你不應該有你的頁面上的元素具有相同的ID,這將導致問題。 –

回答

1

你實際上show當無線電荷蘭國際集團的表值是Non。將其更改爲hide()代替:

if (radio_value == 'Oui') { 
    $("#reprint_oui").show("slow"); 
} else if(radio_value == 'Non') { 
    $("#reprint_oui").hide("slow"); // you have show() here 
} 

DEMO

+0

謝謝,它的工作 – larin555

0

if-else的結果顯示reprint_oui元素。

隱藏元素時, 「非」

http://jsfiddle.net/25KkU/1/

還要確保您使用三聯 「=」 javascript中比較時:

if(a===b) 
{}