Javascript 确认是否选中了radiobox

Javascript 确认是否选中了radiobox,javascript,html,Javascript,Html,目前,我想检查是否仅使用html和javascript检查radiobox。当我单击单选按钮时,我希望弹出一条javascript警报消息,说明此特定按钮已被单击,但什么也没有出现 有人能解决为什么我的代码有错误吗 function checked() { if(document.getElementById("s1").checked) { alert("Small"); } if(document.getElementById("s2").check

目前,我想检查是否仅使用html和javascript检查radiobox。当我单击单选按钮时,我希望弹出一条javascript警报消息,说明此特定按钮已被单击,但什么也没有出现

有人能解决为什么我的代码有错误吗

function checked()
{
if(document.getElementById("s1").checked)
{
  alert("Small");
}               
    if(document.getElementById("s2").checked)
    {
   alert("Medium");
}
if(document.getElementById("s3").checked)
{
   alert("Large");
}
if(document.getElementById("s4").checked)
{
   alert("Extra Large");
}
}

<input type="radio" id="s1" name="shirtsize" value="small" onchange="checked()"/>Small
<input type="radio" id="s2" name="shirtsize" value="medium" onchange="checked()"/>Medium
<input type="radio" id="s3" name="shirtsize" value="large" onchange="checked()"/>Large
<input type="radio" id="s4" name="shirtsize" value="extralarge" onchange="checked()"/>Extra Large
已检查函数()
{
if(document.getElementById(“s1”).已选中)
{
警惕(“小”);
}               
if(document.getElementById(“s2”).已选中)
{
警惕(“中等”);
}
if(document.getElementById(“s3”).已选中)
{
警报(“大型”);
}
如果(选中文档getElementById(“s4”))
{
警报(“超大”);
}
}
小的
中等
大的
特大型

非常感谢您的输入

将函数名从
checked()
更改为
radio\u checked()
,因为checked是保留关键字,将导致错误

这里有标记:

<input type="radio" id="s1" name="shirtsize" value="small" onchange="radio_checked()"/>Small

出现错误是因为选中的
是复选框和单选按钮的属性,并且您正在使用相同的名称创建一个用户定义的函数,即
选中()
,这将导致错误,其余代码将不会执行。

将函数名称从
选中()更改为
单选()
as checked为保留关键字,将导致错误

这里有标记:

<input type="radio" id="s1" name="shirtsize" value="small" onchange="radio_checked()"/>Small

出现错误是因为选中了
复选框和单选按钮的属性,并且您正在使用相同的名称创建用户定义的函数,即
选中()
,这将导致错误,您的其余代码将不会执行。

您必须进行以下更改 html


您必须进行以下更改 html


试试这个,它会给你想要的

var rad = document.getElementsByTagName('input');
var value;
for (var i = 0; i < rad.length; i++) {
    if (rad[i].type === 'radio' && rad[i].checked) {

        alert("checked");     
    }
}
var-rad=document.getElementsByTagName('input');
var值;
对于(变量i=0;i
试试这个,它会给你想要的

var rad = document.getElementsByTagName('input');
var value;
for (var i = 0; i < rad.length; i++) {
    if (rad[i].type === 'radio' && rad[i].checked) {

        alert("checked");     
    }
}
var-rad=document.getElementsByTagName('input');
var值;
对于(变量i=0;i
您可以试试这个:

普通JS

脚本

function radio_checked()
{
    // ......... rest of code .........
}
var show = function(v){
    alert(v + ' was clicked');
};
$(function(){
    $("#s1, #s2, #s3, #s4").on('change', function(e){
        e.stopPropagation();
        alert($(this).val() + ' was clicked');
    });
})
html

<input type="radio" id="s1" name="shirtsize" value="small" onchange="show(this.value)"/>Small
<input type="radio" id="s2" name="shirtsize" value="medium" onchange="show(this.value)"/>Medium
<input type="radio" id="s3" name="shirtsize" value="large" onchange="show(this.value)"/>Large
<input type="radio" id="s4" name="shirtsize" value="extralarge" onchange="show(this.value)"/>Extra Large
<input type="radio" id="s1" name="shirtsize" value="small"/>Small
<input type="radio" id="s2" name="shirtsize" value="medium"/>Medium
<input type="radio" id="s3" name="shirtsize" value="large"/>Large
<input type="radio" id="s4" name="shirtsize" value="extralarge"/>Extra Large
html

<input type="radio" id="s1" name="shirtsize" value="small" onchange="show(this.value)"/>Small
<input type="radio" id="s2" name="shirtsize" value="medium" onchange="show(this.value)"/>Medium
<input type="radio" id="s3" name="shirtsize" value="large" onchange="show(this.value)"/>Large
<input type="radio" id="s4" name="shirtsize" value="extralarge" onchange="show(this.value)"/>Extra Large
<input type="radio" id="s1" name="shirtsize" value="small"/>Small
<input type="radio" id="s2" name="shirtsize" value="medium"/>Medium
<input type="radio" id="s3" name="shirtsize" value="large"/>Large
<input type="radio" id="s4" name="shirtsize" value="extralarge"/>Extra Large
小
中等
大的
特大型
您可以试试这个:

普通JS

脚本

function radio_checked()
{
    // ......... rest of code .........
}
var show = function(v){
    alert(v + ' was clicked');
};
$(function(){
    $("#s1, #s2, #s3, #s4").on('change', function(e){
        e.stopPropagation();
        alert($(this).val() + ' was clicked');
    });
})
html

<input type="radio" id="s1" name="shirtsize" value="small" onchange="show(this.value)"/>Small
<input type="radio" id="s2" name="shirtsize" value="medium" onchange="show(this.value)"/>Medium
<input type="radio" id="s3" name="shirtsize" value="large" onchange="show(this.value)"/>Large
<input type="radio" id="s4" name="shirtsize" value="extralarge" onchange="show(this.value)"/>Extra Large
<input type="radio" id="s1" name="shirtsize" value="small"/>Small
<input type="radio" id="s2" name="shirtsize" value="medium"/>Medium
<input type="radio" id="s3" name="shirtsize" value="large"/>Large
<input type="radio" id="s4" name="shirtsize" value="extralarge"/>Extra Large
html

<input type="radio" id="s1" name="shirtsize" value="small" onchange="show(this.value)"/>Small
<input type="radio" id="s2" name="shirtsize" value="medium" onchange="show(this.value)"/>Medium
<input type="radio" id="s3" name="shirtsize" value="large" onchange="show(this.value)"/>Large
<input type="radio" id="s4" name="shirtsize" value="extralarge" onchange="show(this.value)"/>Extra Large
<input type="radio" id="s1" name="shirtsize" value="small"/>Small
<input type="radio" id="s2" name="shirtsize" value="medium"/>Medium
<input type="radio" id="s3" name="shirtsize" value="large"/>Large
<input type="radio" id="s4" name="shirtsize" value="extralarge"/>Extra Large
小
中等
大的
特大型

您是否尝试了“onclick”而不是“onchange”事件?我尝试了,它给出了相同的结果!Strigid您是否尝试过“onclick”而不是“onchange”事件?我尝试过,它给了我相同的结果!StrigidisThanks,这非常有效,每次我点击按钮都会收到输入,谢谢!谢谢,这工作得很好,我每次点击按钮都会收到输入,谢谢!