![](/img/trans.png)
[英]Uncheck a checkbox by checking a radio button, using pure javascript
[英]How to display the table by checking the checkbox using pure JavaScript?
当复选框被选中但它不起作用时,我需要显示一个表格。 您可以在下面找到我的脚本、复选框和表格
<script>
function checked() {
var checkBox = document.getElementById("check");
var concession = document.getElementById("hidden");
if (checkBox.checked == true){
concession.style.display = "block";
} else {
concession.style.display = "none";
}
}
</script>
<input type = "checkbox" name = "discount" id = "check" onclick = "checked()">
<table id = "hidden" class = "center" style = "display:none">
<tr>
<th>102-2 Taxable income is reduced by 400AZN</th>
<th></th>
<th><input type = "radio" name = "400"></th>
</tr>
<tr>
<th>102-3 Taxable income is reduced by 200AZN</th>
<th></th>
<th><input type = "radio" name = "200"></th>
</tr>
<tr>
<th>102-4 Taxable income is reduced by 100AZN</th>
<th></th>
<th><input type = "radio" name = "100"></th>
</tr>
<tr>
<th>102-5 Taxable income is reduced by 50AZN</th>
<th></th>
<th><input type = "radio" name = 50"></th>
</tr>
</table>
我该如何解决这个问题?
当您使用像onclick
这样的内联处理程序时,您需要分配一个函数,所以onclick="checked"
是正确的方法
作为旁注,应该首选通过addEventListener()
分配事件
<input type="checkbox" name="discount" id="check">
<table id="hidden" class="center" style="display:none">
...
</table>
<script>
var concession = document.getElementById("hidden");
document.getElementById("check").addEventListener('click', function() {
concession.style.display = (this.checked)? "block" : "none";
});
</script>
最后,值得注意的是,JS 根本不是必需的,因为您只能将 CSS 用于具有给定标记的此类任务
#check + table { display: none }
#check:checked + table { display: block }
也许checked()
被限制了,改变你的函数名:
function changed() { var checked = document.getElementById("check").checked; var concession = document.getElementById("hidden"); if (checked) { concession.style.display = "block"; } else { concession.style.display = "none"; } }
<input type="checkbox" name="discount" id="check" onChange="changed()"> <table id="hidden" class="center" style="display:none"> <tr> <th>102-2 Taxable income is reduced by 400AZN</th> <th></th> <th><input type="radio" name="400"></th> </tr> <tr> <th>102-3 Taxable income is reduced by 200AZN</th> <th></th> <th><input type="radio" name="200"></th> </tr> <tr> <th>102-4 Taxable income is reduced by 100AZN</th> <th></th> <th><input type="radio" name="100"></th> </tr> <tr> <th>102-5 Taxable income is reduced by 50AZN</th> <th></th> <th> <input type="radio" name="50"></th> </tr> </table>
您遇到此问题,因为checked
是保留名称。 尝试将函数名称重命名为OnCheckboxClicked
其他名称以解决此问题。
演示:
function OnCheckboxClicked() { var checkBox = document.getElementById("check"); var concession = document.getElementById("hidden"); if (checkBox.checked == true) { concession.style.display = "block"; } else { concession.style.display = "none"; } }
<input type="checkbox" name="discount" id="check" onclick="OnCheckboxClicked()"> <table id="hidden" class="center" style="display:none"> <tr> <th>102-2 Taxable income is reduced by 400AZN</th> <th></th> <th><input type="radio" name="400"></th> </tr> <tr> <th>102-3 Taxable income is reduced by 200AZN</th> <th></th> <th><input type="radio" name="200"></th> </tr> <tr> <th>102-4 Taxable income is reduced by 100AZN</th> <th></th> <th><input type="radio" name="100"></th> </tr> <tr> <th>102-5 Taxable income is reduced by 50AZN</th> <th></th> <th> <input type="radio" name=5 0 "></th> </tr> </table>
您必须向复选框添加一个事件侦听器:
var checkbox = document.querySelector("#check");
checkbox.addEventListener( 'change', function() {
var concession = document.getElementById("hidden");
if(this.checked) {
concession.style.display='block';
} else {
concession.style.display='none';
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.