Javascript 对于无线电输入类型,onclick事件不在表单标记内工作

Javascript 对于无线电输入类型,onclick事件不在表单标记内工作,javascript,Javascript,我一直坚持我认为愚蠢的东西。但现在我尝试了每一种组合,但都没能找出到底是怎么回事 当我将输入类型放入表单中时,onclick inside input type radio不起作用。但当我删除表单标签时,它就起作用了。我还看到了一段代码,其中onclick在表单标记内工作,但在我编写时不工作 这不起作用: <html> <head> <script> function action(str){ document.getEleme

我一直坚持我认为愚蠢的东西。但现在我尝试了每一种组合,但都没能找出到底是怎么回事

当我将输入类型放入表单中时,onclick inside input type radio不起作用。但当我删除表单标签时,它就起作用了。我还看到了一段代码,其中onclick在表单标记内工作,但在我编写时不工作

这不起作用:

<html>
<head>
    <script>
    function action(str){
        document.getElementById("test1").innerHTML=str
    }
    </script>
</head>
<body>
    <form>
        <input type="radio" value="ping" name="rb" onclick="action(this.value)">Ping
        <input type="radio" value="card" name="rb" onclick="action(this.value)">Card
        <input type="radio" value="temp" name="rb" onclick="action(this.value)">Temprature
        <p id="test1">radio will be displayed here</p>
    </form>
</body>
</html>

功能动作(str){
document.getElementById(“test1”).innerHTML=str
}
发出砰的声响
卡片
温度
此处将显示收音机


当我删除表单时,它会工作。

更改函数的名称
操作

试试这个:

<html>
<head>
<script type="text/javascript">
    function action123(str){
        document.getElementById("test1").innerHTML=str
    }
</script>
</head>
<body>
<form>
    <input type="radio" value="ping" name="rb" onclick="action123(this.value);">Ping
    <input type="radio" value="card" name="rb" onclick="action123(this.value);">Card
    <input type="radio" value="temp" name="rb" onclick="action123(this.value);">Temprature
    <p id="test1">radio will be displayed here</p>
</form>
</body>
</html>

函数action123(str){
document.getElementById(“test1”).innerHTML=str
}
发出砰的声响
卡片
温度
此处将显示收音机


您不应该再使用内联事件函数了。使用事件侦听器:

radios = document.getElementsByName("rb");
for(i=0; i<radios.length; i++) {
    radios[i].addEventListener('click', function(e){
       document.getElementById("test1").innerHTML = e.target.value;
    });
}
radios=document.getElementsByName(“rb”);
对于(i=0;i,“action”似乎是表单的阻塞名称空间

试一试

<script>   
function otherName(str){
document.getElementById("test1").innerHTML=str;
}
</script>
 <form name="form">
    <input type="radio" value="ping" name="rb" onclick="otherName(this.value)" />Ping
    <input type="radio" value="card" name="rb" onclick="otherName(this.value)" />Card
    <input type="radio" value="temp" name="rb" onclick="otherName(this.value)" />Temprature
<p id="test1">radio will be displayed here</p>
</form>

函数otherName(str){
document.getElementById(“test1”).innerHTML=str;
}
发出砰的声响
卡片
温度
此处将显示收音机


编辑您的帖子,将代码放入代码高亮显示,同时显示HTML。