如何根据用户选择的单选按钮更改要显示的内容
how to change content to display according to radio button that user select?
我将创建 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' /> This is content A of request</p>
<p><input type='radio' name='content_type' value='2' /> This is content B of request</p>
<p><input type='radio' name='content_type' value='3' /> This is content C of request</p>
<div id='show'></div>
爪哇语
$(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;
}
});
我上面的代码不起作用。 任何人都可以帮我展示这个问题吗?
提前致谢
你错误地使用了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
您的选择器是错误的。
1)而不是#content_type
,你应该使用input[name=content_type]
2)另外,从getElementById中删除#
:
document.getElementById('#show')
应该是
document.getElementById('show')
或者将其更改为使用 jQuery:
$('#show').html("1st radio button");
这是修改后的 html ....我只是在您的消息中添加了一个跨度
<p><input type='radio' class="rad" name='content_type' value='1' /> <span >This is content A of request</span></p>
<p><input type='radio' class="rad" name='content_type' value='2' /> <span>This is content B of request</span></p>
<p><input type='radio' name='content_type' value='3' /> <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();
});
});
这是演示
使用以下代码:
$('input[name=content_type]').on('change', function(){
或
$('input[type=radio]').on('change', function(){
#content_type
意味着您正在选择一个 id 名为 content_type 的元素,并且您没有这样的任何元素。
之后,您的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;
}
});
});
相关文章:
- Javascript:单选按钮,并显示基于vaule的图像
- Javascript:单击单选按钮时显示图像
- 可以't使用AJAX和PHP显示单选按钮值
- ASP.NET 显示单选按钮列表和下拉列表的进度条/加载
- 显示单选按钮的编号 即时检查
- 从数据表中选择一行以显示单选按钮值
- 有没有办法模糊/突出显示单选按钮的文本
- 离子 - 在弹出窗口中显示单选按钮
- jQuery 显示单选按钮,如果 .length 大于 5
- 根据选择框中的选项显示单选按钮
- 显示单选按钮总值
- 创建一个弹出窗口,并使用javascript显示单选按钮列表
- 如何使用javascript实现隐藏和显示单选按钮
- 隐藏和显示单选按钮与复选框
- PHP显示单选按钮单击上的文本框
- 如何使用firebug调试并显示单选按钮的值
- 使用javascript隐藏/显示单选按钮列表项
- 在页面加载上突出显示单选按钮
- 根据下拉菜单的值显示单选按钮
- 在js表单上,如何隐藏/显示单选按钮(一组3个按钮中的一个或两个)取决于在上面字段中输入的数字的值