-2

ばかげていると思うことに固執しました。しかし、今、私はすべての組み合わせを試しましたが、何が起こっているのかを知ることができません.

フォーム内に入力タイプを配置すると、入力タイプ ラジオ内の onclick が機能しません。しかし、フォームタグを削除すると機能します。また、フォーム タグ内で 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>

フォームを削除すると機能します。

4

3 に答える 3

2

関数の名前を変更するaction

これを試して :

<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>
于 2013-08-21T10:01:45.077 に答える
1

インライン イベント関数はもう使用しないでください。イベント リスナーを使用します。

radios = document.getElementsByName("rb");
for(i=0; i<radios.length; i++) {
    radios[i].addEventListener('click', function(e){
       document.getElementById("test1").innerHTML = e.target.value;
    });
}

JSFiddle

于 2013-08-21T10:03:11.943 に答える
0

「アクション」は、フォームの名前空間がブロックされているようです

のように試してみてください

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

http://jsfiddle.net/waeqT/

于 2013-08-21T10:06:29.723 に答える