2012-03-18 42 views
0

在單選按鈕上選擇是我試圖顯示一個div,但我無法使它工作。我的代碼:JavaScript單選按鈕和顯示

<input id="y" type="radio" name="experience" value="y"/>Yes 
<input id="n" type="radio" name="experience" value="n"/>No 

<style type="text/css"> 
.desc { display: none; } 
</style> 

<div id="yes" class="desc"> 
<p>You have Wielding Experience!</p> 
</div> 

<div id="no" class="desc"> 
<p>You have no Wielding Experience!</p> 
</div> 

<script type="text/javascript"> 
$(document).ready(function(){   
$("input[name='experience']").click(function(){ 
    if ($("input[name='experience']:checked").val() == 'y') 
     $("#yes").show();  
    }); 
    }); 
</script> 

我在哪裏出錯了?

+0

拋棄'@',還有'name ='location''在您的HTML中輸入的位置? – mVChr 2012-03-18 12:50:33

+0

放棄@的噢,那不存在了,我會編輯它 - 雖然同樣的問題! – gdrules 2012-03-18 12:51:57

回答

2

更換radioinput

$("input[name='experience']").click(function() { 
    if ($("input[name='experience']:checked").val() == 'y') { 
     $("#yes").show(); 
    } 
}); 

另見this example

PS:更換所有<input ...>...</input><input ... />...

+0

該網站看起來不錯!但即使使用該代碼,我的Chrome仍然無法正常工作 – gdrules 2012-03-18 12:59:38

+0

我的例子也適用於chrome,也就是說,你能告訴我你的更新代碼嗎? – scessor 2012-03-18 13:03:40

+0

我編輯了我原來的帖子,它基本上和你發佈的代碼一樣 – gdrules 2012-03-18 13:08:24

1

我會寫

如果($( 「輸入[名稱= '經驗']:勾選」)VAL()== 'Y')

,而不是

如果( $( 「輸入[@名稱= '位置']:勾選」)VAL()== 'Y')

即不帶@的輸入,並使用無線電的實際名稱

也許你。意思是

$(document).ready(function(){   
    $("input[name='experience']").click(function(e){ 
    if ($this).val() == 'y') { 
     $("#yes").show();  
     $("#no").hide();  
    } 
    else { 
     $("#no").show();  
     $("#yes").hide();  
    } 
    }); 
}); 
1
$('#y').click(function(){ 
    $('#yes').show(); 
}); 
2

選擇必須使用輸入一個單選按鈕:廣播不僅僅是無線電這樣的

$(「輸入:無線​​電[@名稱= '經驗'] 「)