繁体   English   中英

JavaScript单选按钮和显示

[英]JavaScript Radio Buttons and Show

在单选按钮上选择“是”时,我试图显示一个div,但无法使其正常工作。 我的代码:

<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>

我要去哪里错了?

input替换radio

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

另请参见此示例

PS:将所有<input ...>...</input>替换为<input ... />...

要选择单选按钮,您必须使用input:radio,而不仅仅是这样的单选

$(“ input:radio [@ name ='experience']”)

我会写

如果($(“ input [name ='experience']:checked”)。val()=='y')

代替

如果($(“ input [@ name ='location']:checked”)。val()=='y')

即输入不带@并使用收音机的实际名称

但也许你是说

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM