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

jquery 判断checkbox是否选中

WBOY
Release: 2016-06-01 09:54:48
Original
1326 people have browsed it
第一种方法:
Copy after login
<code class="language-javascript">if ($("#checkbox-id").get(0).checked) { 
​}</code>
Copy after login

第二种方法:
Copy after login
<code class="language-javascript">if($('#checkbox-id').is(':checked')) { 
}</code>
Copy after login

第三种方法:
Copy after login
<code class="language-javascript">if ($('#checkbox-id').attr('checked')) {   
​}</code>
Copy after login

实例:

本实例获取所有checkbox的选中值。

<code class="language-javascript"><input type="checkbox" name="myck" value="php">php
<input type="checkbox" name="myck" value="mysql" checked>mysql
<input type="checkbox" name="myck" value="jquery">jquery
<input type="checkbox" name="myck" value="java">java
<input type="button" id="btn" value="jquery 获取多个checkbox选中项的值">
<script src="/Public/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#btn").click(function(){
        $('input:checkbox').each(function() { 
            if ($(this).get(0).checked) { 
                alert($(this).val()); 
            } 
       }); 
		
    })
})
</script></code>
Copy after login

在线运行

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!