我有一个页面,里面有很多复选框。我想编写一个将在单击 ckeckbox 时调用的函数,它确定复选框是否被选中。
<input type="checkbox" onclick="toggleVis('id', this);"/> ID
<input type="checkbox" onclick="toggleVis('edit', this);" checked="checked"/> Edit
<input type="checkbox" onclick="toggleVis('last', this);"/> Last
注意一些复选框开始被选中。
我认为一定有一种方法可以根据传递的引用来执行此操作,所以我将此值作为参数传递。
function toggleVis(name, checkbox)
{
//if(checkbox.checked())
console.log('checked');
if($('.'+name).css('display') != "none")
$('.'+name).css('display', 'none');
else
$('.'+name).css('display', 'table-cell');
}
我愿意使用 jQuery。
最佳答案
你很接近。
if(checkbox.checked) {
console.log('checked');
//...
}
没有 checked()
方法,但是有一个 checked
属性。请注意,您的代码可能仅适用于点击。也许 onchange
会更好?
关于javascript - 给定一个复选框对象输出它的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4450959/