[英]custom checkbox doesn't check
I'm trying to customize my checkboxes, I've tried a few different tutorials, but for every single one of them, I'm still unable to check or uncheck.我正在尝试自定义我的复选框,我尝试了一些不同的教程,但对于其中的每一个,我仍然无法选中或取消选中。
Here's my code so far : https://jsfiddle.net/NecroSyri/behgLox0/10/到目前为止,这是我的代码: https : //jsfiddle.net/NecroSyri/behgLox0/10/
I'm using vanilla css and html, there's js too but it's unrelated to the checkboxes, I've tried to comment it just in case, same result.我正在使用 vanilla css 和 html,也有 js,但它与复选框无关,我试图对其进行评论,以防万一,结果相同。
redrawCheckboxes(); //this function create the elements that replaces the checkboxes, but that's all
Anyone has an idea what's keeping the click to check/uncheck the inputs ?任何人都知道是什么保持点击以检查/取消选中输入? Thanks谢谢
Ok I found another way to actually change the checkbox design instead of redrawing it :好的,我找到了另一种方法来实际更改复选框设计而不是重新绘制它:
https://jsfiddle.net/NecroSyri/behgLox0/17/ https://jsfiddle.net/NecroSyri/behgLox0/17/
most of it is here :大部分在这里:
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
I had a look at your jsFiddle and tried a few things.我看了你的 jsFiddle 并尝试了一些东西。
I commented those lines in your CSS我在你的 CSS 中评论了这些行
.tree input[type=checkbox] {
display: none;
}
to understand how you binded your checkbox to your custom ones.了解如何将复选框绑定到自定义复选框。
I discovered that when you click on the original checkbox, your customs ones are well binded.我发现当您单击原始复选框时,您的海关复选框已绑定良好。 But not the opposite.但不是相反。 Indeed, I see no trace of an onClick
listener is you JS.确实,我看不到任何onClick
侦听器的痕迹是您的 JS。 But I'm really a noob in Vanilla, so maybe I'm missing something.但我真的是香草菜鸟,所以也许我错过了一些东西。
But in my opinion, I would add something like this in your redraw()
method :但在我看来,我会在你的redraw()
方法中添加这样的东西:
function redrawCheckboxes(){
document.querySelectorAll('.tree input[type="checkbox"]').forEach(function(cbox){
var cmark = document.createElement('i');
var newbox = cbox;
var parentElem = cbox.parentElement;
parentElem.prepend(cmark);
parentElem.prepend(newbox);
cmark.classList.add('checkmark');
cmark.addEventListener("click", function(e) {
cmark.previousElementSibling.checked = !cmark.previousElementSibling.checked;
});
});
} }
I'm sure you'll find a way to clean those 3 last lines of code.我相信你会找到一种方法来清理最后 3 行代码。 But still : it should do the job.但仍然:它应该完成这项工作。
Happy coding快乐编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.