0

我想要做的是,通过单击该函数中的单选按钮从另一个函数调用一个函数。这是我迄今为止工作的代码 -

<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 时,我只希望出现一个按钮。最初,两者都设置为“隐藏”可见性。请帮帮我。对不起,如果看起来不清楚,我尽力了.. 谢谢。

4

3 回答 3

1

尝试改变:

rad1.onclick = mainalso();
rad2.onclick = onlyapp();

到:

rad1.onclick = mainalso;
rad2.onclick = onlyapp;

就目前而言,您正在将调用functions的结果分配给onclickof rad1rad2在这种情况下将是undefined. 通过删除()您将function正确分配。

为了确保button1button2在您被调用时可用并设置为正确的值,您function可以将代码更改为:

...
newP.appendChild(button2);
area.appendChild(newP);
(function (b1, b2) {
    rad1.onclick = function () {
        b1.style.visibility="visible";
        b2.style.visibility="visible";
    };
    rad2.onclick = function () {
        b2.style.visibility="visible";
        b1.style.visibility="hidden";
    };
})(button1, button2);
于 2012-07-13T08:08:38.343 回答
1

你应该做这个:

rad1.onclick = mainalso;
rad2.onclick = onlyapp;

编辑:我试过这个,它对我有用。查看示例

window.onload = function() {
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();
}
于 2012-07-13T08:09:12.590 回答
0

您必须定义您正在使用的 newP 和区域:
area.appendChild(newP);

由于标签 HTML 是这样使用的:

<input type="radio" name="dooropt" id="rb1" value="y" onClick="mainalso()"/> 

所以你可以使用:

rad1.onClick=mainalso;

于 2012-07-13T08:47:22.557 回答