Home  >  Article  >  Web Front-end  >  jquery 判断checkbox是否选中

jquery 判断checkbox是否选中

WBOY
WBOYOriginal
2016-06-01 09:54:481335browse
第一种方法:
<code class="language-javascript">if ($("#checkbox-id").get(0).checked) { 
​}</code>

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

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

实例:

本实例获取所有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>

在线运行

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