我想获取未选中框的值
HTML:
<br>
<input class="computer_type" id="laptop" type="checkbox" checked> Laptops
<br>
<input class="computer_type" id="desktop" type="checkbox" checked> Desktops
javascript:
function get_computer_type() {
$('.computer_type:not(:checked)').map(function() {
return $(this).attr('id');
}).get();
}
$('.computer_type').change(function() {
console.log(get_computer_type());
});
不幸的是,这返回未定义,我不知道为什么。
最佳答案
该函数没有返回任何内容,因此返回未定义(在 JavaScript 中,如果函数未显式返回任何内容,则默认情况下返回未定义)。您必须从函数中返回从map()
获取的结果。
function get_computer_type() {
return $('.computer_type:not(:checked)').map(function() {
return $(this).attr('id');
}).get();
}
$('.computer_type').change(function(){
console.log(get_computer_type());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<br>
<input class="computer_type" id="laptop" type="checkbox" checked> Laptops
<br>
<input class="computer_type" id="desktop" type="checkbox" checked> Desktops
关于javascript - 获取未选中复选框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54920376/