简体   繁体   English

更改选中复选框的 INPUT TEXT 的值

[英]Change value of an INPUT TEXT checking a checkbox

Why is this not working?为什么这不起作用? It is supposed to work this way: if the checkbox is checked, it should take the content of a variable txt and write it to the input with id of show.它应该以这种方式工作:如果复选框被选中,它应该获取变量 txt 的内容并将其写入 id 为 show 的输入。

 function myFunction() { if (document.getElementById('myRadio').checked) { var txt = "Done"; document.getElementById("show").innerHTML = 'txt'; } }
 <p>If the radio checkbox is checked the text from the variable text is written into input line with the ID of show</p> <input type="checkbox" onclick="myFunction()" id="myRadio"> <br> <br> <input type="text" class="form-control" id="show">

You need to set the value property of the input element, not the innerHTML :您需要设置 input 元素的value属性,而不是innerHTML

 function myFunction() { if (document.getElementById('myRadio').checked){ var txt = "Done"; document.getElementById("show").value = txt; } else { document.getElementById("show").value = ''; } }
 <p>If the radio checkbox is checked the text from the variable text is written into input line with the ID of show</p> <input type="checkbox" onclick="myFunction()" id="myRadio"> <br> <br> <input type="text" class="form-control" id="show">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM