jQuery判断checkbox复选框是否已选中
发布于:2022-03-01 10:20:43
次阅读
<input type="checkbox" name="fruit" value="apple" />苹果<input type="checkbox" name="fruit" value="orange" />橘子<input type="checkbox" name="fruit" value="banana" />香蕉<input type="checkbox" name="fruit" value="grape" />葡萄<button id="bt1">判断选中</button><script> $(function (){ $("#bt1").click(function (){ var checkVal='';//查找已选的值 $("input[name='fruit']:checkbox").each(function(){ if($(this).prop('checked')){ checkVal+=$(this).val()+','; } }) console.log(checkVal); var flag=false;//判断是否选中值 $("input[name='fruit']:checkbox").each(function(){ if($(this).prop('checked')){ flag=true; } }) console.log(flag) if(flag){ alert('有被选中'); }else{ alert('没有选中任何选项'); } }) })</script>