[英]How to know if a checkbox is checked onmouseover corresponding label
I want to perform certain actions when hovering over checkbox-labels, but only when the checkboxes are checked.我想在将鼠标悬停在复选框标签上时执行某些操作,但仅限于选中复选框时。
How can I retrieve that information?我怎样才能检索到这些信息?
HTML HTML
<input clrcheckbox="" type="checkbox" id="checks">
<label for="checks">Check</label>
JS JS
document.querySelector('label').onmouseover = () => {
//so here should be something like:
//if(checkbox is checked) {console.log('checked')}
console.log('over');
}
Here a solution with jQuery这里有一个 jQuery 的解决方案
<input clrcheckbox="" type="checkbox" id="checks">
<label for="checks">Check</label>
<script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
<script>
$(document).ready(function(){
var isChecked = function(){
if ($('#checks').filter(':checked').length > 0){
console.log('Do some action')
}
}
$('#checks').mouseover(isChecked)
$('label').mouseover(isChecked)
})
</script>
https://codesandbox.io/s/strange-sunset-nkq5q https://codesandbox.io/s/strange-sunset-nkq5q
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.