doutu7123 2019-07-30 11:17
浏览 95

单击复选框后,addEventListener无法正常工作

I have a problem with my javascript. Everytime I click the checkbox, the eventlistener is not working. Screenshot

I used this code for 2 PHP pages. The one is working, and the other one is not working. This is not an external javascript. It's supposed to enable the buttons in a spinner and the textboxes in the form.

<div class="checkbox-custom checkbox-success">
    <input type="checkbox" name="inclusion[]" id="inclusionName<?php echo $inclusionId ?>" value="<?php echo $inclusionId ?>">
    <label for="<?php echo $inclusionId ?>">&nbsp;&nbsp;&nbsp;<?php echo $inclusionName ?></label>
</div>

I tried to change the function name and it still isn't working.

<script>
var inclusionSelect<?php echo $inclusionId ?> = document.getElementById('inclusionName<?php echo $inclusionId ?');

inclusionSelect<?php echo $inclusionId ?>.addEventListener("click", function selectCheck<?php echo $inclusionId ?>() {

if (inclusionSelect<?php echo $inclusionId ?>.checked == true) {
    totalPrice = totalPrice + totalUnitPrice<?php echo $inclusionId ?>;
    document.getElementById("totalPrice").value = totalPrice.toFixed(2);
    document.getElementById('unitCount<?php echo $inclusionId ?>').disabled=false;
    document.getElementById('unitCountUp<?php echo $inclusionId ?>').disabled=false;
    document.getElementById('unitPrice<?php echo $inclusionId ?>').disabled=false;
    document.getElementById('totalUnitPrice<?php echo $inclusionId ?>').disabled=false;


    if (unitCount<?php echo $inclusionId ?> >= 1) {
        document.getElementById('unitCountDown<?php echo $inclusionId ?>').disabled=false;
    }

    if (unitCount<?php echo $inclusionId ?> >= 99) {
        document.getElementById('unitCountUp<?php echo $inclusionId ?>').disabled=true;
    }
}
else {
    totalPrice = totalPrice - totalUnitPrice<?php echo $inclusionId ?>;
    document.getElementById('totalPrice').value = totalPrice.toFixed(2);
    document.getElementById('unitCount<?php echo $inclusionId ?>').disabled=true;
    document.getElementById('unitCountUp<?php echo $inclusionId ?>').disabled=true;
    document.getElementById('unitCountDown<?php echo $inclusionId ?>').disabled=true;
    document.getElementById('unitPrice<?php echo $inclusionId ?>').disabled=true;
    document.getElementById('totalUnitPrice<?php echo $inclusionId ?>').disabled=true;
}

}, false);
</script>
  • 写回答

1条回答 默认 最新

  • duanluo5096 2019-07-30 12:40
    关注

    (I take the liberty to post this as an answer, while it isn't. I just need the space.)

    Please post the actual HTML produced by this script, makes it MUCH easier to debug.

    But one thing I find awkward is:

    id="inclusionName<?php echo $inclusionId ?>"
    

    versus

    label for="<?php echo $inclusionId ?>"
    

    The second should be also "inclusionName<?php echo $inclusionId ?>" for your label to match.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错