🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>JS获取复选框中当前选中的值</title> </head> <script type="text/javascript"> window.onload = function() { select_item(); } function select_item() { var aFruit=document.getElementsByName("checkfruit"); var oSelectAll=document.getElementById("selectAll"); var oP=document.getElementById("content"); var str=""; //全选/全不选 oSelectAll.onchange=function() { if(this.checked) { for(var i=0;i<aFruit.length;i++) { aFruit[i].checked=true; str+=aFruit[i].value+" "; } oP.innerText="你选择的瓜是:"+str; }else { for(var i=0;i<aFruit.length;i++) { aFruit[i].checked=false; } oP.innerText="你没有选择任何瓜"; str=""; } } //单个点击复选框 var str1=""; for(var i=0;i<aFruit.length;i++) { aFruit[i].onchange=function() { if(this.checked) { str1=str1+this.value+" "; }else { str1=str1.replace(this.value,""); } oP.innerText="你选择的瓜是:"+str1; } } } </script> <body> <div> <label><input type="checkbox" id="selectAll"/>全选/全不选:</label><br /><br /> <label><input type="checkbox" name="checkfruit" value="冬瓜"/>冬瓜</label> <label><input type="checkbox" name="checkfruit" value="南瓜"/>南瓜</label> <label><input type="checkbox" name="checkfruit" value="西瓜"/>西瓜</label> <label><input type="checkbox" name="checkfruit" value="北瓜"/>北瓜</label> </div> <p id="content"></p> </body> </html> ```