如何检查是否选择了v-select

时间:2019-12-23 16:24:03

标签: vuejs2 v-select

在vuejs中如何正确使用v-select检查元素是否被选中 在选项数组中包含代码/标签数组?

我尝试过:

<v-select 
  v-model="selection_filter_priority" 
  label="label" 
  :options="taskPriorityLabels" 
  id="filter_priority"
  name="filter_priority" 
  class="form-control editable_field" 
  placeholder="Select all"
></v-select>
console.log('-11 typeof this.selection_filter_priority::')
console.log(typeof this.selection_filter_priority)
console.log(this.selection_filter_priority)

if (typeof this.selection_filter_priority == 'object' && typeof this.selection_filter_priority != null) {
  filter_priority = this.selection_filter_priority.code // But if option is not selected(null) I got error here:
}

哪种方法有效?

“ vue”:“ ^ 2.6.10”, “ vue-select”:“ ^ 3.2.0”,

1 个答案:

答案 0 :(得分:1)

您的代码未正确测试this.selection_filter_priority是否为null。要检查对象是否为null,请改用if (this.selection_filter_priority === null)

请参见下面的演示

var nullValue = null;
var objectValue = {};
var numberValue = 1;

console.log('null');
check(nullValue);
console.log('\n');

console.log('object');
check(objectValue);
console.log('\n');

console.log('number');
check(numberValue);
console.log('\n');

function check(x) {
  if (x === null) {
    console.log('is null');
  }

  if (typeof x == 'object' && typeof x != null) {
  // is same as if (typeof x == 'object')
    console.log('null or object');
  }
  
  if (typeof x != null) {
    console.log('always true');
  }
}