我怎样才能让这个 javascript 函数一直工作?(未捕获的 TypeError:无法读取 null 的属性)
2022-07-18
73
我尝试创建两个数字输入,每个数字输入都有自己的自定义“+”和“-”按钮。但是,使用我当前的代码,按钮功能似乎只在某些时候起作用。在其他情况下,我收到
Uncaught TypeError: Cannot read properties of null (reading 'value') at HTMLButtonElement.<anonymous>
错误。
这是 HTML:
<div class="col">
<div class="row">
<div class="input-group mx-auto w-50">
<div class="input-group-prepend">
<button class="btn btn-dark btn-sm minus" data-id="one"
type="button"><i class="fa fa-minus"></i></button>
</div>
<input type="number" id="one"
class="form-control form-control-sm text-center" value="0">
<div class="input-group-prepend">
<button class="btn btn-dark btn-sm plus" data-id="one" type="button"><i
class="fa fa-plus"></i></button>
</div>
</div>
</div>
<div class="row">
<div class="input-group mx-auto w-50">
<div class="input-group-prepend">
<button class="btn btn-dark btn-sm minus" data-id="two"
type="button"><i class="fa fa-minus"></i></button>
</div>
<input type="number" id="two"
class="form-control form-control-sm text-center" value="0">
<div class="input-group-prepend">
<button class="btn btn-dark btn-sm plus" data-id="two" type="button"><i
class="fa fa-plus"></i></button>
</div>
</div>
</div>
</div>
这是 Javascript:
<script>
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll('.minus').forEach(item => {
item.addEventListener('click', event => {
var input_id = event.target.getAttribute('data-id')
console.log('Minus button clicked for input ' + input_id)
var quantityInput = document.getElementById(input_id);
var currentQuantity = parseInt(quantityInput.value);
quantityInput.value = currentQuantity - 1
})
})
document.querySelectorAll('.plus').forEach(item => {
item.addEventListener('click', event => {
var input_id = event.target.getAttribute('data-id')
console.log('Plus button clicked for input ' + input_id)
var quantityInput = document.getElementById(input_id);
var currentQuantity = parseInt(quantityInput.value);
quantityInput.value = currentQuantity + 1
})
})
})
这是单击几个按钮后的输出示例:
2个回答
event.target
可能是按钮内的跨度,因此代码可能找不到
data-id
属性。也许可以在不使用
event.target
的情况下获取 id。可以使用
item
或
event.currentTarget
。
dqhendricks
2022-07-18
点击事件发生在
<i class="fa fa-minus"></i>
您可以使用
Event.currentTarget
代替
event.target
document.addEventListener("DOMContentLoaded", () =>
{
document.querySelectorAll('.minus').forEach(item =>
{
item.onclick = event =>
{
let
input_id = event.currentTarget.dataset.id
, quantityInput = document.getElementById(input_id)
, currentQuantity = parseInt(quantityInput.value)
;
console.log('Minus button clicked for input ' + input_id)
quantityInput.value = currentQuantity - 1
}
})
document.querySelectorAll('.plus').forEach(item =>
{
item.onclick = event =>
{
let
input_id = event.currentTarget.dataset.id
, quantityInput = document.getElementById(input_id)
, currentQuantity = parseInt(quantityInput.value)
;
console.log('Plus button clicked for input ' + input_id)
quantityInput.value = currentQuantity + 1
}
})
})
您也可以简单地执行以下操作:
document.addEventListener("DOMContentLoaded", () =>
{
document
.querySelectorAll('button.minus, button.plus')
.forEach( btn => // this is the button
{
btn.onclick = () => // no event argument needed
{
let
input_Qte = document.getElementById( btn.dataset.id )
, adding = btn.classList.contains('plus') ? +1 : -1
;
input_Qte.value = +input_Qte.value + adding // use +input_Qte.value for integer conversion
}
})
})
Mister Jojo
2022-07-18