代码
提交代码
<form> <div> <label> <input type="checkbox" name="skill" checked="checked" value="JavaScript"> JavaScript </label> <label> <input type="checkbox" name="skill" value="c++"> C++ </label> <label> <input type="checkbox" name="skill" checked="checked" value="Java"> Java </label> </div> </form> <div id="result"></div> <script> var checkboxes = document.getElementsByName('skill'); var skills = []; checkboxes.forEach(function(checkbox) { if (checkbox.getAttribute('checked')) { skills.push(checkbox.value); } }); document.getElementById('result').innerHTML = '选中的技能:' + skills.join('、'); </script>
运行结果