【发布时间】:2019-05-22 17:40:38
【问题描述】:
我的页面上有一个 select2 下拉列表,其中填充了数据库中表格中的国家/地区。一个用户可以选择多个国家,形成一个数组。我想在数组中搜索两个值。例如:
var selvalues = $(this).val(); //["AS", "US", "CA"]
在 dropwdown 的 change 事件中,如果 "US" 是其中一个值,并且数组还包含一个不是 "US" 的值,则抛出错误。
到目前为止我在 jQuery 中尝试过的一些事情:
$('#countriesSelect').on('change', function(){
var selvalues = $(this).val();
console.log(selvalues .some(x => x !== 'US' && x == 'US'));
if(selvalues.includes('US') || !selvalues.includes('US')){
console.log('You cannot choose a foreign country when current selection is US');
}
if($.inArray("US",selvalues) != -1){
$('#usregions').show().fadeIn();
if($.inArray("US",selvalues) != -1){}
} else {
$('#usregions').hide().fadeOut();
}
$.each(selvalues, function( index, value ) {
console.log()
if(value == 'US' && value != 'US'){
console.log('You cannot choose a foreign country when current selection is US');
}
});
});
我希望它根据上述标准抛出错误。
【问题讨论】:
标签: jquery arrays jquery-select2 dropdown