2014-09-24 49 views
-2

爲什麼我的功能在單選按鈕上隱藏/顯示元素不起作用?爲什麼我的功能在單選按鈕上隱藏/顯示元素基礎不起作用?

http://jsfiddle.net/qmzz9x7h/

<script> 
    $(document).ready(function(){ 
     $('input[type="radio"]').click(function(){ 
      if($(#radio_id).attr("value")=="first"){ 
       $("#second_checkbox").hide();     
       $("#first_checkbox_display").show(); 
      } 
      if($(#radio_id).attr("value")=="second"){ 
       $("#first_checkbox_display").hide();     
       $("#second_checkbox").show(); 
      } 
     }); 
    }); 
</script> 
+0

什麼是_#radio_id_? – 2014-09-24 08:19:26

+0

我想檢查單選按鈕'id =「radio_id」' – 2014-09-24 08:20:16

+0

的值您可以使用雙ID來播放無線電項目。通過使用相同的名稱,不相同的ID組號碼按鈕 – PieterSchool 2014-09-24 08:20:57

回答

1
<script> 
    $(document).ready(function(){ 
     $(".radio_id").click(function(){ 
      if($(this).attr("value")=="first"){ 
       $("#second_checkbox").hide();     
       $("#first_checkbox_display").show(); 
      } 
      if($(this).attr("value")=="second"){ 
       $("#first_checkbox_display").hide();     
       $("#second_checkbox").show(); 
      } 
     }); 
    }); 
</script> 

給你的單選按鈕類 「radio_id」

+0

不工作謝謝.. – 2014-09-24 08:22:58

+0

現在試試吧..... – void 2014-09-24 08:23:52

+0

不行.......你能不能請給我一些小提琴。 – 2014-09-24 08:25:24

2

你的小提琴是錯誤的,拿來看短期和改進的版本:

$(document).ready(function(){ 
    $('input[type="radio"]').click(function(e){ 
     var _val = $(this).val(); 
     var _id = $(this).attr('id'); // Grap the ID 

     $("#second_checkbox, #first_checkbox_display").hide(); 

     console.log(_val); 
     if(_val == "first"){ 
      $("#"+_val+"_checkbox_display").show(); 
     } 
     else if(_val =="second"){  
      $("#"+_val+"_checkbox").show(); 
     } 
    }); 
}); 

Demo

+0

我也想使用單選按鈕id =「radio_id」'的id。 – 2014-09-24 08:26:56

+0

@papmooo看看更新的代碼 – 2014-09-24 08:30:00

相關問題