如果我选中多个复选框,我想打印所有在复选框中打勾的值。但是,它只打印我检查的第一个值。 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;
}
}
答案 0 :(得分:1)
使用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>
答案 1 :(得分:0)
您的功能应如下所示
function check_skills(){
var selected_skills = $('.chk:checked');
if (selected_skills.length > 0)
{
$("#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;
}
}
每次调用函数时,都使用选择器:checked
和.chk
类来获取所有选中的复选框。
答案 2 :(得分:0)
我已经找到问题的答案。事实证明,我需要在每个复选框的名称中添加[]。因此,当我在技能上添加[]时,它会打印复选框中的所有选中值。顺便说一句,感谢所有帮助过我的人。
<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>
[skills] => Array
(
[0] => Web Development
[1] => Business Intelligence/Analytics
[2] => Networking
)