【发布时间】:2020-01-30 16:53:38
【问题描述】:
我有一个文本框,如果未选中复选框,我希望在单击复选框时显示该文本框并消失,但是我的代码当前在页面加载时显示文本框,但是一旦您选择然后取消选择复选框,它就会消失应该的。有什么想法吗?
Javascript:
function TMDFunction() {
// Get the checkbox
var checkBox = document.getElementById("tmd_checkbox");
// Get the output text
var text = document.getElementById("test");
// If the checkbox is checked, display the output text
if (checkBox.checked == true) {
text.style.display = 'block';
} else {
text.style.display = 'none';
}
}
<input type="checkbox" class="product" name="targetedmobiledisplay" value="0.08" id="tmd_checkbox" onclick="TMDFunction()">
Targeted Mobile Display
<br>
Test: <input id="test" type="text" value="0.00" data-total="0" />
【问题讨论】:
-
只需在文本框中添加简单的 CSS 以将其设置为不显示...
#test{display:none}或调用TMDFunction()onload 页面 -
旁注;
text.style.display = (checkBox.checked ? 'block' : 'none');
标签: javascript jquery html checkbox textbox