简体   繁体   English

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

[英]how to change content to display according to radio button that user select?

I'm going to create 3 radio buttons for user to select. 我将创建3个单选按钮供用户选择。 Once, user click on that button, it will show user the description of that radio button that user selected. 一旦用户单击该按钮,它将向用户显示该用户选择的单选按钮的描述。

For example 例如

- 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

My code 我的密码

<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 使用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;
        }
    });

My code above didn't work. 我上面的代码不起作用。 Can anyone one help me to show this problem? 谁能帮助我显示此问题?

Thank in advance 预先感谢

You are incorrectly using jQuery selectors. 您错误地使用了jQuery选择器。 Also you need to close the $(document).ready(). 另外,您需要关闭$(document).ready()。 Right code: 正确的代码:

$(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 的jsfiddle

use this code: 使用此代码:

$('input[name=content_type]').on('change', function(){

OR 要么

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

#content_type means you are selecting an element that has the id named content_type. #content_type表示您正在选择一个ID为content_type的元素。 and you don't have such any element. 而且您没有任何元素。

After that your JS code will be: 之后,您的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;
        }
    });
});

Your selector is wrong. 您的选择器有误。

1) Instead of #content_type , you should use input[name=content_type] 1) 您应该使用input[name=content_type]代替#content_type

2) Also, remove the # from getElementById: 2) 另外,从getElementById中删除#

document.getElementById('#show')

should be 应该

document.getElementById('show')

or change it to use jQuery: 或将其更改为使用jQuery:

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

Here is the modified html ....i simply added a span to your messages 这是经过修改的html .... i只是在您的消息中添加了一个跨度

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

And here is the jQuery 这是jQuery

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

And here is the demo 这是演示

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

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