简体   繁体   English

单击复选框后,addEventListener不起作用

[英]After clicking checkbox, addEventListener is not working

I have a problem with my javascript. 我的JavaScript有问题。 Everytime I click the checkbox, the eventlistener is not working. 每次单击复选框时,事件侦听器都无法正常工作。 Screenshot 屏幕截图

I used this code for 2 PHP pages. 我将此代码用于2个PHP页面。 The one is working, and the other one is not working. 一个正在工作,而另一个则不工作。 This is not an external javascript. 这不是外部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>

(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. 请发布此脚本生成的实际HTML,使其更易于调试。

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. 第二个也应该是"inclusionName<?php echo $inclusionId ?>"以便标签匹配。

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

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