我想知道选择表单
中未选择的元素。 (此选择
是使用for循环
动态创建的)。
我知道如何知道所选的值,
$('#bbdd_btn').click(function(){
$("#select_nodes").each(function() {
var selected = $(this).children(":selected").val();
console.log(selected);
});
});
但我想知道未选中的。我读过一些帖子,发现 this answer但我不知道如何正确地做到这一点。我试试这个,
$('#bbdd_btn').click(function(){
$("#select_nodes").each(function() {
var selected = $(this).children(":selected").val();
console.log(selected);
var not_selected = $(this).children:not(":selected").val();
//var no_selected = $(this):not(":selected").val();
//var no_selected = $(this).children(:not(":selected").val();
console.log(not_selected);
});
});
哪种方法是正确的?
非常感谢。
最佳答案
您可以通过这种方式更改代码
$('#bbdd_btn').click(function(){
$("#select_nodes option").each(function() {
if($(this).is(':selected')){
console.log(selected);
}else{
console.log(not_selected);
}
});
});
关于javascript - 使用jquery了解选择表单中未选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59343489/