3 回答
TA贡献1815条经验 获得超6个赞
e.value 中有什么?
它不包含复选框的直接值。这工作正常
console.log(e.value)
var value = 'quant_' + 1;
var x = document.getElementById(value);
console.log(x, 'hello');
https://jsfiddle.net/5dom7wx8/16/
TA贡献1875条经验 获得超5个赞
您应该分配一个更通用的事件侦听器,而不是像这里那样使用内联处理程序。如果使用querySelectorAll就不需要使用getElementById,这在很多情况下是非常不方便的。由于上面的 javascript 函数似乎还没有做太多,也许您可以尝试这样的方法
document.querySelectorAll('input[type="checkbox"]').forEach( chk=>{
chk.addEventListener( 'click', function(e){
if( this.checked ){
alert( this.value )
}
})
});
TA贡献1824条经验 获得超6个赞
尽管有代码本身,为了使它符合您的解释,您应该将要更改的输入(我假设)的 id 传递给方法 updateInputsLabels:
<td><input type="checkbox" name="product[]" value="<?=$row['product_id']?>" onchange="updateInputsLabels('<?=$row['product_id']?>')" checked></td>
并改变你的方法:
function updateInputsLabels(inputId){
if(typeof inputId='string' && inputId.length > 0){
let fullId = 'quantity_' + inputId;
let element = document.getElementById(fullId );
console.log(fullId );
}else {
console.error("not a string or empty");
}
}
- 3 回答
- 0 关注
- 207 浏览
添加回答
举报