Home > Article > Web Front-end > Use js to determine whether the checkbox is selected and use js to operate the checkbox
We often encounter this problem in our projects: use js to determine whether the checkbox is selected and use js to operate the checkbox.
In fact, these requirements are very simple. Here are the ways to use native js and jQuery to complete these requirements.
<input type="checkbox" id="test" class="test">同意<script> // 获取checkbox元素 var box=document.getElementById("test"); // 判断是否被拒选中,选中返回true,未选中返回false alert(box.checked);</script>
<input type="checkbox" id="test" class="test">同意<script> // 获取checkbox元素 var box=document.getElementById("test"); // 判断是否被拒选中,选中返回true,未选中返回false alert(box.checked);</script>
<input type="checkbox" id="test" class="test">同意<script> // 获取checkbox元素 var box=document.getElementById("test"); // 判断是否被拒选中,选中返回true,未选中返回false box.checked=false;</script>
<input type="checkbox" id="test" class="test">同意<script> // 选中返回true,未选中返回false $('#test').is(":checked"); // 选中返回true,未选中返回false;一定要注意,这里不可以使用attr("checked")来判断 $("#test").prop("checked")</script>
<input type="checkbox" id="test" class="test">同意<script> $("#test").prop("checked","trure") $("#test").attr("checked","true")</script>
<input type="checkbox" id="test" class="test">同意<script> $("#test").prop("checked","false") $("#test").attr("checked","false")</script>
Related articles:
jquery Determine whether the checkbox is selected
js How to determine whether the checkbox is selected_Basic knowledge
Related videos:
JS#jQuery width and height understanding and application
The above is the detailed content of Use js to determine whether the checkbox is selected and use js to operate the checkbox. For more information, please follow other related articles on the PHP Chinese website!