Javascript 我该如何检查“是否”;复选框“;是否选中,如果选中,是否显示元素?

Javascript 我该如何检查“是否”;复选框“;是否选中,如果选中,是否显示元素?,javascript,Javascript,选中复选框时,复选框的checked属性为true 所以只需将if(chk.checked==1)更改为if(chk.checked==true) 编辑 另外,如果选中复选框,请将和idTexttoshow添加到要显示的输入中 这里有一些地方不对劲 首先让我们修复html元素 这需要一个id: This code will check if Checkbox is checked and if so, it will show the CSS Hidden Textbox. How do I m

选中复选框时,复选框的
checked
属性为
true

所以只需将
if(chk.checked==1)
更改为
if(chk.checked==true)

编辑

另外,如果选中复选框,请将和id
Texttoshow
添加到要显示的输入中


这里有一些地方不对劲

首先让我们修复html元素

这需要一个id:

This code will check if Checkbox is
checked and if so, it will show the
CSS Hidden Textbox. How do I make it
so it will Do so in that manner?
如果要在
if
else
块中放入多条语句,则它们需要用大括号括起来

<input onclick="validate(this)" type="check">

功能验证(chk){
如果(chk.checked==1)document.getElementById('Texttoshow').style.display=“block”;
否则{
提醒(“你没有检查!让我帮你检查一下。”)
chk.checked=1;
}
}
我可能会对布尔语句使用
true/false
,但是
1
在js中也很好

<input type="text" value="hey" style="display:none" id="Texttoshow">
<input onclick="validate(this)" type="check">
<script>
function validate(chk) {
    if (chk.checked == 1) document.getElementById('Texttoshow').style.display = "block";
    else {
        alert("You didn't check it! Let me check it for you.")
        chk.checked = 1;
    }
}
</script>