<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>
<input type="text" value="hey" style="display:none"><input oncheck="validate(this.id)" type="check">
输出:document.getElementById('Texttoshow')是空的
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?
答案 0 :(得分:3)
这里有一些问题。
首先修复你的html元素。
这需要一个id:
<input type="text" value="hey" style="display:none" id="Texttoshow">
这里你需要传递元素而不是(不存在的)id。它也应该是onclick
而不是oncheck
<input onclick="validate(this)" type="check">
如果您在if
或else
块中放置了多个语句,则需要包含大括号。
<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>
我可能会使用true/false
作为布尔语句,但{j}中的1
也没问题。
答案 1 :(得分:1)
当checked
其他true
checked
属性为false
所以只需将if (chk.checked == 1)
更改为if (chk.checked === true)
修改强>
如果选中该复选框,请添加并标识要显示的输入Texttoshow
。
<input type="text" value="hey" style="display:none" id='Texttoshow'>