【问题标题】:Checkbox selection triggered from another function从另一个函数触发的复选框选择
【发布时间】:2020-05-15 06:20:02
【问题描述】:

我正在尝试编写一个代码,当我单击 DIV 时,复选框标记我,第二次单击时,它标记

颜色发生变化,DIV 发出咔哒声。但是自动选择和取消选中复选框不起作用

我有错误:

Uncaught TypeError: selectedValue.prop is not a function at HTMLDivElement.document.getElementById.onclick

我有带有此代码的 GSP 模板:

<div id="${item.key}">
    <input id="Position_${item.key}" type="checkbox" />  ${index + 1}. ${item.key} 
</div>

和脚本在同一个文件中:

<script>
    document.getElementById('${item.key}').onclick = function () {
        let selectedValue = document.getElementById('Position_${item.key}');

        if (this.style.backgroundColor == 'lightgreen') {
            changeColor(this, 'white');
            selectedValue.prop('checked', false);
        } else {
            changeColor(this, 'lightgreen');
            selectedValue.prop('checked', true);
        }
    };
</script>

【问题讨论】:

    标签: javascript jquery html checkbox gsp


    【解决方案1】:

    prop 方法是 jQuery 元素包装器的一部分。你有一个简单的HTMLElement 对象

    需要直接设置属性:

    selectedValue.checked = false;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-19
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      相关资源
      最近更新 更多