[英]Check and Uncheck Checkbox
為什么當我選中此復選框時,如果我未選中它卻沒有任何反應,它可以正常工作
<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>
幾件事。 GetElementsByTagName
是錯誤的函數調用,該方法用於通過元素的實際HTML標簽獲取元素數組。 請改用GetElementsByName
。 另外,此調用將返回一個數組,因此您需要指定它是哪個索引(它將是索引0)。 由於checked
已經是布爾值,因此您無需指定== true
。
替換if (document.getElementsByTagName('manualentry').checked = true)
與if (document.getElementsByName('manualentry')[0].checked)
您在if條件中忘記了=
:
if (document.getElementsByTagName('manualentry').checked = true) {
嘗試
if (document.getElementsByTagName('manualentry').checked == true) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.