Home > Web Front-end > JS Tutorial > body text

Use js to determine whether the checkbox is selected and use js to operate the checkbox

php是最好的语言
Release: 2018-08-01 10:27:44
Original
2432 people have browsed it

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.

1. Use native JavaScript to determine whether the checkbox is selected

<input type="checkbox" id="test" class="test">同意<script>
    // 获取checkbox元素
    var box=document.getElementById("test");      
    // 判断是否被拒选中,选中返回true,未选中返回false
    alert(box.checked);</script>
Copy after login
Copy after login

2. Use native JavaScript to determine whether the checkbox is selected

<input type="checkbox" id="test" class="test">同意<script>
    // 获取checkbox元素
    var box=document.getElementById("test");      
    // 判断是否被拒选中,选中返回true,未选中返回false
    alert(box.checked);</script>
Copy after login
Copy after login

3. Use native JavaScript to remove Selected checkbox

<input type="checkbox" id="test" class="test">同意<script>
    // 获取checkbox元素
    var box=document.getElementById("test");      
    // 判断是否被拒选中,选中返回true,未选中返回false
    box.checked=false;</script>
Copy after login

4. Use jQuery to determine whether the checkbox is selected

<input type="checkbox" id="test" class="test">同意<script>
    // 选中返回true,未选中返回false
    $(&#39;#test&#39;).is(":checked");    // 选中返回true,未选中返回false;一定要注意,这里不可以使用attr("checked")来判断
    $("#test").prop("checked")</script>
Copy after login

5. Use jQuery settings to select the checkbox

<input type="checkbox" id="test" class="test">同意<script>
     $("#test").prop("checked","trure")
     $("#test").attr("checked","true")</script>
Copy after login

6. Use jQuery settings to remove the selection Checkbox box

<input type="checkbox" id="test" class="test">同意<script>
     $("#test").prop("checked","false")
     $("#test").attr("checked","false")</script>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!