如何根据用户选择的单选按钮更改显示内容?

5
我将创建3个单选按钮供用户选择。一旦用户点击该按钮,它将显示用户所选单选按钮的描述。
例如:
- if user select first radio button ==> it will show the description under that
                                        radio button

- if user select second radio button ==> it will show the description under that
                                        radio button

- if user select third radio button ==> it will show the description under that
                                        radio button

我的代码

<p><input type='radio' name='content_type' value='1' />&nbsp;This is content A of request</p>
<p><input type='radio' name='content_type' value='2' />&nbsp;This is content B of request</p>
<p><input type='radio' name='content_type' value='3' />&nbsp;This is content C of request</p>
<div id='show'></div>

Javascript

$(document).ready(function(){
    $('#content_type').on('change', function(){
    var n = $(this).val();
    switch(n)
    {
            case '1':
                  document.getElementById('#show').innerHTML="1st radio button";
                  break;
            case '2':
                  document.getElementById('#show').innerHTML="2nd radio button";
                  break;
            case '3':
                  document.getElementById('#show').innerHTML="3rd radio button";
                  break;
        }
    });

我之前的代码不起作用了,有人能帮我解决一下这个问题吗?

提前感谢。

4个回答

4
您正在错误地使用jQuery选择器。此外,您需要关闭$(document).ready()。正确的代码如下:
$(document).ready(function(){
    $('input[name=content_type]').on('change', function(){
    var n = $(this).val();
    switch(n)
    {
            case '1':
                  $('#show').html("1st radio button");
                  break;
            case '2':
                  $('#show').html("2nd radio button");
                  break;
            case '3':
                  $('#show').html("3rd radio button");
                  break;
        }
    });
});

JSfiddle


0
请使用以下代码:
$('input[name=content_type]').on('change', function(){

或者

$('input[type=radio]').on('change', function(){

#content_type 意味着你正在选择一个具有名为 content_type 的 id 的元素,但是你没有任何这样的元素。

之后你的 JS 代码将是:

$(document).ready(function(){
    $('input[type=radio]').change(function(){
    var n = $(this).val();
    switch(n)
    {
            case '1':
                  $('#show').html("1st radio button");
                  break;
            case '2':
                  $('#show').html("2nd radio button");
                  break;
            case '3':
                  $('#show').html("3rd radio button");
                  break;
        }
    });
});

0

您的选择器有误。

1)应该使用 input[name=content_type],而不是 #content_type

2)此外,请从 getElementById 中删除 #

document.getElementById('#show')

应该是

document.getElementById('show')

或者改用jQuery:

$('#show').html("1st radio button");

0

这是修改后的HTML代码...我只是在您的消息中添加了一个标签

 <p><input type='radio' class="rad" name='content_type' value='1' />&nbsp;<span >This is content A of request</span></p>
<p><input type='radio' class="rad" name='content_type' value='2' />&nbsp;<span>This is content B of request</span></p>
<p><input type='radio'  name='content_type' value='3' />&nbsp;<span>This is content C of request</span></p>
<div id='show'></div>

这里是jQuery

$(document).ready(function(){
    $('span').hide();
    $('input[name="content_type"]').on('change',function(){
       $('span').hide();
       $(this).next().show();
    });
});

这里是演示


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接