繁体   English   中英

选中(取消)复选框时显示其他文本

[英]Display another text when CheckBox is (un)checked

应该很简单。 您有以下文字:

您的尖叫声:哎呀!

当您选中下面的复选框时,

尖叫“是”!

“哇!”一词应更改为“是!”:

您的尖叫声:是的!

所以我的身体是(我想用一个函数来做):

Your scream: <script>GiveMeSomeScream()</script>!<hr>
<input type="checkbox" name="whatever" id="ScreamYay"> Scream "yay" instead!

但是我在使用实际脚本时遇到了问题。 这是我首先尝试的方法:

function GiveMeSomeScream()
{  if (document.GetElementById('ScreamYay').checked == "true")
{  document.write("Yay");
}  else
{  document.write("Whoops");
}}

但这是行不通的。 所以我这样扩展它:

var yay = document.getElementById('ScreamYay').checked
function GiveMeSomeScream()
{  if (yay == "true")
{  document.write("Yay");
}  else
{  document.write("Whoops");
}}

现在,它每次都会给出“ Whoops”(即使我默认选中此复选框),也就差不多了。

我知道这很荒谬,我什至无法处理一个复选框,但是我仍然很乐意提供帮助。

if (yay == "true")更改为if (yay)

document.getElementById('elementID').checked返回的值将是一个布尔值。 这将在此处进一步说明。 要使用它,您可以简单地使用@Alex建议的if(yay === true)if(yay)对值进行测试。 对于使用document.write ,您需要包括html标记(标记)像这样

你可以像下面这样
您必须在复选框标签上添加onchange函数。
还有document.GetElementById是错误的
document.getElementbyId是正确的
最后
document.getElementById(“ ScreamYay”)。checked ==“ true”错误
document.getElementById(“ ScreamYay”)。checked == true是正确的

function GiveMeSomeScream()
{  if (document.getElementById('ScreamYay').checked == true)
{  document.write("Yay");
}  else
{  document.write("Whoops");
}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM