Home  >  Article  >  Web Front-end  >  Use js to determine whether the checkbox is selected and use js to operate the checkbox

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

php是最好的语言
php是最好的语言Original
2018-08-01 10:27:442468browse

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>

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>

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>

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>

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>

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>

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!

Statement:
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