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

use*_*520 5 html javascript jquery

我将创建 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
Run Code Online (Sandbox Code Playgroud)

我的代码

<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>
Run Code Online (Sandbox Code Playgroud)

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;
        }
    });
Run Code Online (Sandbox Code Playgroud)

我上面的代码不起作用。任何人都可以帮我展示这个问题吗?

预先感谢

小智 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;
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

JS小提琴