通过'onclick'事件从另一个函数调用一个函数

时间:2012-07-13 08:04:14

标签: javascript

我要做的是,通过单击该功能中的单选按钮从另一个函数调用函数。这是我到目前为止所使用的代码 -

<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时,我想要出现两个按钮(button1和button2),但是在点击rad2时我只想出现一个按钮。两者都首先被设置为“隐藏”的可见性。 请帮帮我。对不起,如果它似乎不清楚,我尽我所能.. 谢谢。

3 个答案:

答案 0 :(得分:1)

尝试更改:

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

要:

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

目前您将调用function的结果分配给onclickrad1的{​​{1}},在这种情况下rad2 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);

答案 1 :(得分:1)

你应该这样做:

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

编辑:我试过这个,它对我有用。见example

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();
}

答案 2 :(得分:0)

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

由于标签HTML的使用方式如下:

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

所以你可以使用:

rad1.onClick = mainalso;