[英]Text added with innerHTML when checkbox is checked not removing when unchecked
When you check the checkbox I am trying to make the text 'Yes, I like F1' appear in the div with the id="print-f1", which is working. 当您选中复选框时,我正在尝试使文本“是,我喜欢F1”出现在div中,且id为“ print-f1”,这是有效的。
However the text doesn't disappear when you uncheck the checkbox. 但是,当您取消选中复选框时,文本不会消失 。
Also I noticed if I set the checkbox by default to checked in the HTML, it doesn't load the page with 'Yes, I like F1' adding to the id="print-f1" div either. 另外我还注意到,如果我默认情况下将复选框设置为在HTML中选中,则不会以“是的,我喜欢F1”加载页面,也不会添加到id =“ print-f1” div中。
I am not using innerHTML = '';
我没有使用
innerHTML = '';
to remove the text because I read using removeChild
is much more efficient . 删除文本,因为我使用
removeChild
读取的效率更高 。
I am also adding whatever name is entered inside the input into the first grey div with the id="print-name" that part is working. 我还将在输入中输入的任何名称添加到第一个灰色div中,该部分正在使用id =“ print-name”。
https://jsfiddle.net/r8qbfdmy/ https://jsfiddle.net/r8qbfdmy/
JS JS
function myForm() {
var nameForm = document.forms.nameform;
var printF1 = document.getElementById('print-f1');
nameForm.name.onkeyup = function() {
document.getElementById('print-name').innerHTML = nameForm.name.value;
};
nameForm.likef1.addEventListener("focus", printF1func)
function printF1func() {
if (printF1.checked === false) {
while (printF1.firstChild) printF1.removeChild(printF1.firstChild);
} else {
printF1.innerHTML = nameForm.likef1.value;
};
};
};
myForm();
HTML HTML
<form name="nameform">
<input name="name" class="form__input rounded-4" placeholder="Enter your name">
<label>
<input type="checkbox" name="likef1" value="Yes, I like F1"> Do you like F1?
</label>
<div id="print-name" class="rounded-4"></div>
<div id="print-f1" class="rounded-4"></div>
</form>
CSS CSS
.rounded-4 {
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.form__input {
border-width: 2px;
border-style: solid;
border-color: #989898;
width: 200px;
line-height: 40px;
color: #989898;
padding: 0 12px;
font-size: 16px;
outline: none;
margin: 20px;
}
#print-name {
background-color: #989898;
width: 200px;
line-height: 40px;
height: 40px;
color: #fff;
padding: 0 12px;
font-size: 16px;
outline: none;
margin: 0 20px;
}
#print-f1 {
background-color: #989898;
width: 200px;
line-height: 40px;
height: 40px;
color: #fff;
padding: 0 12px;
font-size: 16px;
outline: none;
margin: 20px;
}
Replace this... 取代这个...
nameForm.likef1.addEventListener("focus", printF1func)
function printF1func() {
if (printF1.checked === false) {
while (printF1.firstChild) printF1.removeChild(printF1.firstChild);
} else {
printF1.innerHTML = nameForm.likef1.value;
};
};
... with this... ... 有了这个...
nameForm.likef1.addEventListener("change", printF1func);
function printF1func() {
if (nameForm.likef1.checked) {
printF1.innerHTML = nameForm.likef1.value;
} else {
printF1.innerHTML = '';
}
};
... and it should work fine. ...,它应该可以正常工作。
See also this Fiddle for a demo. 另请参阅此小提琴以获取演示。
In this particular case, I seriously doubt you'll get much of a performance gain (if any at all) when using printF1.removeChild(printF1.firstChild);
在这种特殊情况下,我严重怀疑使用
printF1.removeChild(printF1.firstChild);
时是否会获得很多性能提升(如果有的printF1.removeChild(printF1.firstChild);
instead of printF1.innerHTML = '';
而不是
printF1.innerHTML = '';
If you insist on using printF1.removeChild(printF1.firstChild);
如果您坚持使用
printF1.removeChild(printF1.firstChild);
, however, note that you can drop the while loop, because printF1
will always have exactly one child every time it is executed : 但是,请注意,您可以删除while循环,因为每次执行
printF1
时,它始终只有一个孩子:
nameForm.likef1.addEventListener("change", printF1func);
function printF1func() {
if (nameForm.likef1.checked) {
printF1.innerHTML = nameForm.likef1.value;
} else {
printF1.removeChild(printF1.firstChild);
}
};
See also this Fiddle for a demo. 另请参阅此小提琴以获取演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.