从两个值设置Vue JS选择选项

时间:2019-10-03 11:10:39

标签: javascript vue.js vuejs2 nuxt.js

我有一个Nuxt JS / Vue JS应用程序,该应用程序带有我正在使用getaddress.io构建的地址查找器

我有一个<select>元素,其中有许多县,是从数据对象中提取的。用户键入邮政编码时,应使用API​​中的匹配县设置县<select>

但是,并非所有县都在我的代码中列出或匹配,因此我需要检查从API提取的内容是否与特定字段匹配,否则,请使用地区键。

这是我的JS方法,在找到地址后选择地址时运行:

/**
 * Pick address
 */
addressPickerSelection: function addressPickerSelection(addressKey) {
  const cleanedPostcode = this.formData.AppPostCode.toUpperCase()

  var addressRaw = this.address.addressList[addressKey]
  var cleanAddress = {
    house: (addressRaw['building_number'] != '') ? addressRaw['building_number'] : (addressRaw['building_number'] === '' && addressRaw['line_1']) ? addressRaw['line_1'] : '',
    street: (addressRaw['thoroughfare'] != '') ? addressRaw['thoroughfare'] : (addressRaw['thoroughfare'] === '' && addressRaw['line_1']) ? addressRaw['line_1'] : '',
    town: (addressRaw['town_or_city'] != '') ? addressRaw['town_or_city'] : '',
    county: addressRaw['county']
  }


  this.formData.AppPreviousPostCode = (cleanedPostcode != '') ? cleanedPostcode.replace(/ /g,'') : ''
  this.formData.EmpPostCode = (cleanedPostcode != '') ? cleanedPostcode.replace(/ /g,'') : ''

  this.formData.AppHouseNumber = cleanAddress['house']
  this.formData.AppStreet = cleanAddress['street']
  this.formData.AppTown = cleanAddress['town']
  this.formData.AppCounty = cleanAddress['county']

  this.formData.EmpAddress = cleanAddress['house']
  this.formData.EmpAddress2 = cleanAddress['street']
  this.formData.EmpTown = cleanAddress['town']
  this.formData.EmpCounty = cleanAddress['county']

  if (addressRaw['district'] != '') {
    var countySelectValue = this.formData.AppCounty
    this.formData.AppCounty = addressRaw['district']
    if (this.formData.AppCounty == '') {
      this.formData.AppCounty = countySelectValue
    }
    countySelectValue = ''
  }

  this.address.addressSelected = true
}

cleanAddress['county']可能并不总是与我的县列表中的项匹配,而addressRaw['district']则可能与之匹配,因此在找不到cleanAddress['county']或不匹配的情况下,它应该利用地区

例如:

  1. 我在其中键入英国邮政编码
  2. 我选择了一个地址,但是在我的县县中不存在“ Mid Glamorgan”,它应该利用该地区。

这是我要工作的代码部分,但似乎不起作用:

if (addressRaw['district'] != '') {
    var countySelectValue = this.formData.AppCounty
    this.formData.AppCounty = addressRaw['district']
    if (this.formData.AppCounty == '') {
      this.formData.AppCounty = countySelectValue
    }
    countySelectValue = ''
  }

这是一个常见问题,如果某人不存在,则需要将一个选择项设置为两个可能值之一。

我要去哪里错了?

0 个答案:

没有答案