2012-03-18 98 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

选择必须使用输入一个单选按钮:广播不仅仅是无线电这样的

$(“输入:无线​​电[@名称= '经验'] “)