我该怎么叫收音机按钮呢

How would I call upon a radio button?

本文关键字:收音机 按钮      更新时间:2023-09-26

我想在现有单选按钮响应中的yes/no响应中添加文本,但我不确定如何做到这一点http://jsfiddle.net/pjdicke/zudCf/2/

<p>Yes <input type="radio" name="another" value="anotherYes" /></p>
<p>No <input type="radio" name="another" value="anotherNo" /></p>

我需要添加什么才能根据是/否回答显示文本?

HTML部分的第39行是我引用的代码的来源。我希望是/否的答案有两个基于答案的回答。例如:天空是蓝色的吗?蓝色对你做过什么?但如果答案是=(否)蓝色是一种很棒的颜色

我讨厌匿名函数。虽然他们可以有一席之地,但这大多只是懒惰的编程,imho。

以下是一些代码,希望您能更清楚地理解这些代码。

<!DOCTYPE html>
<html>
<head>
<script>
window.addEventListener('load', mInit, false);
function mInit()
{
    var radioBtns = document.querySelectorAll("input[type=radio]");
    var i, n = radioBtns.length;
    for (i=0; i<n; i++)
    {
        var btnGroupName = radioBtns[i].getAttribute('name');
        if (btnGroupName == 'yesNo')
            radioBtns[i].addEventListener('change', onRbGroup1Change, false);
        else if (btnGroupName == 'gender')
            radioBtns[i].addEventListener('change', onRbGroup2Change, false);
    }
}
function onRbGroup1Change()
{
    document.getElementById('radioResult').innerHTML = "Value of chosen radio button is: " + this.value;
}
function onRbGroup2Change()
{
    document.getElementById('radioResult2').innerHTML = "Gender of chosen radio button is: " + this.value;
}
</script>
<style>
</style>
</head>
<body>
<label>Yes<input type="radio" name="yesNo" value="afirmative"/></label>
<label>No<input type="radio" name="yesNo" value="negative"/></label>
<div id='radioResult'>You need to select a radio-button first!</div>
<label>Girl<input type="radio" name="gender" value="Female"/></label>
<label>Boy<input type="radio" name="gender" value="Male"/></label>
<div id='radioResult2'>You need to select a radio-button first!</div>
</body>
</html>