[英]Check and Uncheck Checkbox
Why when I check the checkbox it works fine when I uncheck it nothing happen 为什么当我选中此复选框时,如果我未选中它却没有任何反应,它可以正常工作
<form method="get">
<table id="row">
<tr><th colspan="2" >Location</th></tr>
<tr><td>Country:</td><td><select id="country" name ="country" style="width:200px"></select></td></tr>
<tr><td>City:</td><td><select name ="city" id ="state"></select></td></tr>
<script language="javascript">
populateCountries("country", "state");
</script>
<tr><td></td><td><input onclick="myFunction()" type="checkbox" name="manualentry" value="manualentry" >Manual Entry</td></tr>
<script>
var table = document.getElementById("row");
function myFunction() {
if (document.getElementsByTagName('manualentry').checked = true) {
document.getElementById("row").deleteRow(2);
var row = table.insertRow(2);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(-1);
cell1.innerHTML = "City:";
cell2.innerHTML = '<input type="text" >';
} else {
document.getElementById("row").deleteRow(2);
var row = table.insertRow(2);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "City:";
cell2.innerHTML = '<select name ="city" id ="state"></select>';
}
}
</script>
<tr ><td colspan="2" align="right" > <input type="submit" value="Submit"></td></tr>
</table>
</form>
A couple things. 几件事。
GetElementsByTagName
is the wrong function call, that method is used to get an array of elements by their actual HTML tag. GetElementsByTagName
是错误的函数调用,该方法用于通过元素的实际HTML标签获取元素数组。 Use GetElementsByName
instead. 请改用
GetElementsByName
。 Also, this call will return an array, so you need to specify which index it is (it will be index 0). 另外,此调用将返回一个数组,因此您需要指定它是哪个索引(它将是索引0)。 Since
checked
is already a boolean value, you do not need to specify == true
. 由于
checked
已经是布尔值,因此您无需指定== true
。
Replace if (document.getElementsByTagName('manualentry').checked = true)
替换
if (document.getElementsByTagName('manualentry').checked = true)
with if (document.getElementsByName('manualentry')[0].checked)
与
if (document.getElementsByName('manualentry')[0].checked)
You forgot a =
in the if condition: 您在if条件中忘记了
=
:
if (document.getElementsByTagName('manualentry').checked = true) {
try 尝试
if (document.getElementsByTagName('manualentry').checked == true) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.