jquery 获取复选框选中的值得办法

来源:互联网 发布:魔兽世界1.12数据库 编辑:程序博客网 时间:2024/05/20 06:06

首先咱们先写一个简单的复选框

<input type="checkbox" value="橘子" name="check">橘子</input>        <input type="checkbox" value="香蕉" name="check">香蕉</input>        <input type="checkbox" value="西瓜" name="check">西瓜</input>        <input type="checkbox" value="芒果" name="check">芒果</input>        <input type="checkbox" value="葡萄" name="check">葡萄</input>                <input type="button" value="方法1" id="b1">

然后咱们就引入jq

<script src = "./js/jquery-1.11.1.min.js" ></script>

开始写

<script type="text/javascript"> $(document).ready(function(){$("#b1").click(function(){$.each($('input:checkbox:checked'),function(){//也可以这么写input[type = checkbox]:checkedalert("你选了:"+$('input:checkbox:checked').length+"个,其中有:"+$(this).val());})//这里是判断你是否选中的复选框var  id_array=new Array();  $('input[name="check"]:checked').each(function(){      id_array.push($(this).val());//向数组中添加元素});   if(id_array.length == 0) { alert("请选择"); }});$("#b2").click(function(){$.each($('input:checkbox'),function(){if(this.checked){  window.alert("你选了:"+                        $('input[type=checkbox]:checked').length+"个,其中有:"+$(this).val());}});});});</script>


原创粉丝点击