简体   繁体   English

我如何确保在 JavaScript 中单击任何复选框时,输入字段变为 0

[英]How do I make sure in JavaScript that when I click on any checkbox, the input field goes to 0

How do I make sure that when I click on any checkbox, the input field goes to 0. Because now it works with the first, but not with the others.我如何确保当我单击任何复选框时,输入字段变为 0。因为现在它适用于第一个,但不适用于其他复选框。

This is the code I have now:这是我现在的代码:

let checkbox = document.getElementById("abs");
let inp = document.getElementById("cijfer");
checkbox.addEventListener('change', function() {
    if (this.checked) {
        inp.value = 0;
    }
});

The first input field does go to 0, but the second one does not.第一个输入字段将 go 变为 0,但第二个输入字段没有。

Picture of the problem:问题图片:

https://i.stack.imgur.com/SGIE6.png

Checkbox and input code in gohtml: gohtml中的复选框和输入代码:

<td><input id="cijfer" type="number" name="grade-{{$s.ID}}" step="0.1" min = "0" max = "10" value="{{(index $.Grades $s.ID).Value}}"></td>
            <td>
              <input id="abs" type="checkbox" name="abs-{{$s.ID}}">
              <label for="abs">ABS</label>
            </td>

you are using id ( getElementById ) and it's an unique attribute!您正在使用id ( getElementById ),它是一个独特的属性!

use "abs" class and "cijfer" class instead!请改用“abs”class 和“cijfer”class!

then find the closest input and make the value zero!然后找到最接近的输入并将值设为零!

<script>
function closest(el, classname) {
   if(el.parentNode){
        if(el.parentNode.className.includes(classname)){
        return el.parentNode;
      }
      else{
        return closest(el.parentNode, classname);
      }
   }
   else{
    return false;
   }
}


document.querySelectorAll('.abs').forEach(function(item) {
    item.addEventListener('change', function() {
       if (this.checked) {
         let closest = closest(this, 'cijfer');
         closest.value = "0";
        }
     }
});


</script>

this was pure js: you may use jquery instead:这是纯 js:您可以改用 jquery:

$('.abs').on('change', function(){
    $(this).prev('input').val('0');
});

暂无
暂无

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

相关问题 在 Javascript 中手动操作输入时,如何确保触发键盘事件? - How do I make sure a keyboard event is firing when manually manipulating an input in Javascript? 我如何才能停止工作,并确保标题在我单击时而不是在单击位置时停止 - how do i get .stop to work and make sure that the heading stops when i click and not where i click 如何确保在输入字段中输入新数字时脚本/<p> 标签刷新? - How can I make sure when I put a new number in input field the script/ <p> tag refreshes? 如何确保至少选中一个复选框? - How do I make sure that at least one checkbox is checked? 单击链接时如何显示字段? (javascript) - How do I display field when I click on a link? (javascript) 如何获取输入字段的值并确保其与列表项的值匹配? - How do I take the value of an input field and make sure that it matches the value of a list item? 如何让输入字段只接受 javaScript 中的字母? - How do I make an input field accept only letters in javaScript? 我如何在 php laravel 中创建一个 javascript 弹出输入字段? - How do i make a javascript popup input field in php laravel? Javascript:单击后如何自动建议将结果移至输入字段 - Javascript: how do I make the results from an autosuggest move into the input field when clicked Javascript:当选中/选中分配给它的单选按钮时,如何使输入/文本字段为必需字段? - Javascript: How do I make an input/text field mandatory(required) when the radio button assigned to it is checked/selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM