Javascript 使用map jquery获取数组中的选项值

Javascript 使用map jquery获取数组中的选项值,javascript,jquery,html-select,splice,Javascript,Jquery,Html Select,Splice,$('btnadd')。单击(添加公告栏); 函数addBulletinBoard(){ var show=$('#show').val(); var-show1; log(“在”+$('#show').val()之前) if(jQuery.inArray(“*”,show)!=-1){ show1=$(“选择#显示选项”).map(函数(){ 返回$(this.val(); }).get(); } console.log(“在“+show”之后) console.log(“在“+show1.s

$('btnadd')。单击(添加公告栏);
函数addBulletinBoard(){
var show=$('#show').val();
var-show1;
log(“在”+$('#show').val()之前)
if(jQuery.inArray(“*”,show)!=-1){
show1=$(“选择#显示选项”).map(函数(){
返回$(this.val();
}).get();
}
console.log(“在“+show”之后)
console.log(“在“+show1.splice(0,2)”之后)
}

选择。。。
全部的
选择1
选择2
提交
只需使用
数组#过滤器()

$('btnadd')。单击(添加公告栏);
函数addBulletinBoard(){
var show=$('#show').val().filter(函数(val){
返回val&&val!='*'
});
log(“之后:”,显示)
}

选择。。。
全部的
选择1
选择2

Submit
splice
将返回包含已删除元素的数组,并更改原始数组。因此
console.log(“after”+show1.splice(0,2))
将显示删除的元素,这些元素不是您真正想要的。只需将
show1.splice(0,2)
移出
console.log()
,它就会按照您的预期工作

$('btnadd')。单击(添加公告栏);
函数addBulletinBoard(){
var show=$('#show').val();
var-show1;
log(“在”+$('#show').val()之前)
if(jQuery.inArray(“*”,show)!=-1){
show1=$(“选择#显示选项”).map(函数(){
返回$(this.val();
}).get();
图1.拼接(0,2)
}
console.log(“在“+show”之后)
console.log(“在“+show1”之后)
}

选择。。。
全部的
选择1
选择2

提交
当我选择/包含在所选的所有选项中时,我需要获取所有选项值。在您的示例中,当我仅选择“全部”时,将不显示任何内容。很抱歉,上面的问题一定没有提到。好吧,如果只选择了所有选项,那么就保留它?不,我的意思是,如果选择了所有选项,则选择除0和1索引选项之外的所有选项。哦……那么,填充所有其他值,即使用户没有选择它们?是这样的。仅选择
all->结果为选项1和选项2
全选和选项1结果仍为选项1和选项2
全选和选项2结果仍为选项1和选项2