作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我选中了 1 个以上的复选框,我想打印我在复选框中勾选的所有值。但是,它只打印我检查的第一个值。 If I checked more than 1 checkbox in this image It only printed one value from the checkbox提前致谢
// HTML code
<strong>IT Skills: </strong><br><br>
<div class="checkboxlist" id="skills">
<input type="checkbox" name="skills" value="Programming/Application Development" class="chk"> Programming/Application Development 
<input type="checkbox" name="skills" value="Project Management" class="chk"> Project Management <br>
<input type="checkbox" name="skills" value="Helpdesk/Technical Support" class="chk"> Helpdesk/ Technical Support     
<input type="checkbox" name="skills" value="Security/Compliance Governance" class="chk"> Security/ Compliance Governance <br>
<input type="checkbox" name="skills" value="Web Development" class="chk"> Web Development         
<input type="checkbox" name="skills" value="Database Administration" class="chk"> Database Administration <br>
<input type="checkbox" name="skills" value="Business Intelligence/Analytics" class="chk"> Business Intelligence/ Analytics    
<input type="checkbox" name="skills" value="Mobile Applications and Device Management" class="chk"> Mobile Applications and Device Management <br>
<input type="checkbox" name="skills" value="Networking" class="chk"> Networking            
<input type="checkbox" name="skills" value="Big Data" class="chk"> Big Data <br>
</div>
<span class="error_form" id="skills_error_message"></span>
//jQuery code
function check_skills(){
var skill = [];
if (skills == 'checked') {
$("#skills_error_message").hide();
$("#skills").css("border-bottom","2px solid #34F458");
} else {
$("#skills_error_message").html("Please select a skill!");
$("#skills_error_message").show();
$("#skills").css("border-bottom","2px solid #F90A0A");
error_skills = true;
}
}
最佳答案
使用 jQuery :checked
选择器
//jQuery code
function check_skills() {
var favorite = [];
$.each($("input[name='skills']:checked"), function() {
favorite.push($(this).val());
});
console.log(favorite)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<strong>IT Skills: </strong><br><br>
<div class="checkboxlist" id="skills">
<input type="checkbox" name="skills" value="Programming/Application Development" class="chk"> Programming/Application Development 
<input type="checkbox" name="skills" value="Project Management" class="chk"> Project Management <br>
<input type="checkbox" name="skills" value="Helpdesk/Technical Support" class="chk"> Helpdesk/ Technical Support     
<input type="checkbox" name="skills" value="Security/Compliance Governance" class="chk"> Security/ Compliance Governance <br>
<input type="checkbox" name="skills" value="Web Development" class="chk"> Web Development         
<input type="checkbox" name="skills" value="Database Administration" class="chk"> Database Administration <br>
<input type="checkbox" name="skills" value="Business Intelligence/Analytics" class="chk"> Business Intelligence/ Analytics    
<input type="checkbox" name="skills" value="Mobile Applications and Device Management" class="chk"> Mobile Applications and Device Management <br>
<input type="checkbox" name="skills" value="Networking" class="chk"> Networking            
<input type="checkbox" name="skills" value="Big Data" class="chk"> Big Data <br>
</div>
<span class="error_form" id="skills_error_message"></span>
<button onclick="check_skills()">
Check
</button>
关于javascript - 如何打印所有选中的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55683459/
我是一名优秀的程序员,十分优秀!