繁体   English   中英

未选中复选框时如何更改标签标签的文本

[英]How to change the text of an Label tag when a checkbox is unchecked

取消选中复选框后,我无法更改标签的文本。

在此先感谢您的帮助。

var checkBox_fruit = document.getElementById("fruit");
var label_fruit = document.getElementById("label_fruits");

checkBox_fruit.addEventListener('change', function() {
      if (this.checked) {
        // Checkbox is checked..

        label_fruit.innerHTML = " fruits 1"; ////// its that who works

      } else {
        // Checkbox is not checked..

        label_fruit.innerHTML = " fruits 2"; ////// its that who not works

      }

您的代码工作正常,您只是忘记添加右括号}); .

请参阅演示。

 var checkBox_fruit = document.getElementById("fruit"); var label_fruit = document.getElementById("label_fruits"); checkBox_fruit.addEventListener('change', function() { if (this.checked) { // Checkbox is checked.. label_fruit.innerHTML = " fruits 1"; ////// its that who works } else { // Checkbox is not checked.. label_fruit.innerHTML = " fruits 2"; ////// its that who not works } });
 <input type="checkbox" id="fruit" /> <label id="label_fruits"></label>

您只需要在最后关闭一些})

 var checkBox_fruit = document.getElementById("fruit"); var label_fruit = document.getElementById("label_fruits"); checkBox_fruit.addEventListener('change', function () { if (this.checked) { // Checkbox is checked.. label_fruit.innerHTML = " fruits 1"; ////// its that who works } else { // Checkbox is not checked.. label_fruit.innerHTML = " fruits 2"; ////// its that who not works } });
 <input type='checkbox' id='fruit'> <label id='label_fruits'>Fruit</label>

暂无
暂无

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

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