我想要做的是,通过单击该函数中的单选按钮从另一个函数调用一个函数。这是我迄今为止工作的代码 -
<script type="text/javascript">
rad1 = document.createElement("input");
rad1.type = "radio";
rad1.name = "dooropt";
rad1.id = "rb1";
rad1.value = "y";
newP.appendChild(rad1);
text1 = document.createTextNode("Yes ");
newP.appendChild(text1);
rad2 = document.createElement("input");
rad2.type = "radio";
rad2.name = "dooropt";
rad2.id = "rb2";
rad2.value = "n";
newP.appendChild(rad2);
text2 = document.createTextNode("No ");
newP.appendChild(text2);
button1 = document.createElement("input");
button1.type = "button";
button1.value = "Open main door";
button1.style.visibility = "hidden";
newP.appendChild(button1);
button2 = document.createElement("input");
button2.type = "button";
button2.value = "Open apartment door";
button2.style.visibility = "hidden";
newP.appendChild(button2);
area.appendChild(newP);
rad1.onclick = mainalso();
rad2.onclick = onlyapp();
}
function mainalso()
{
button1.style.visibility="visible";
button2.style.visibility="visible";
}
function onlyapp()
{
button2.style.visibility="visible";
button1.style.visibility="hidden";
}
</script>
现在,每当单击 rad1 时,我希望 mainalso() 运行,这可能从代码中可以清楚地看出。由于我对 JavaScript 相当陌生,我不知道我在哪里犯了错误,在语法或概念上。此外,第一个函数(在 mainalso() 之前)是 add(),在其中,我在单击正文中的按钮时反复附加相同的内容。我希望很清楚我想要做什么。单击 rad1 时,我希望两个按钮(按钮 1 和按钮 2)都出现,但单击 rad2 时,我只希望出现一个按钮。最初,两者都设置为“隐藏”可见性。请帮帮我。对不起,如果看起来不清楚,我尽力了.. 谢谢。