【发布时间】:2019-08-20 18:51:00
【问题描述】:
好吧,我创建了下面的函数,根据作为参数传递的值返回真或假。如果第一个参数(区域)是手动编写的,则该函数将按预期工作,并将根据其他两个参数的值返回 true 或 false。 我现在正在尝试根据用户的选择来设置参数的值,该值作为下拉列表的值。我用一个函数提取了用户的选择,然后将该函数的返回值 (getUserImputForm()) 作为参数传递给另一个。
我已经尝试将 getUserImputForm() 的返回值存储在另一个变量中,并将其作为参数传递给另一个函数。结果不会是假的或真的,只有未定义的。 我还认为问题可能在于返回值没有像'America'那样用引号发送,我试图将它作为return“'”+ e.target.value +“'”返回。还是不行。
const getUserInputForm = () => {
const formSelector = document.querySelector('#select-region')
formSelector.addEventListener('click', (e)=>{
console.log(e.target.value)
return e.target.value
})
}
const match = async(region, country, capital) => {
const allData = await countryesBringer(region)
const countryHere = ()=> {
const countryCapitalArray = []
allData.forEach((element)=> {
countryCapitalArray.push(element.name.common, element.capital[0])
})
console.log(countryCapitalArray)
// return countryArray.includes(country)
for(var i = 0; i<countryCapitalArray.length; i++){
const index = countryCapitalArray.indexOf(country)
const capitalMatch = countryCapitalArray[index+1]
if(capitalMatch === capital){
return true
} else{
return false
}
}
}
const resolved = countryHere()
console.log(resolved)
return resolved
}
match('Americas', 'United States', 'Washington D.C.').then((value => {
console.log(value)
}))
// The match function will run accordingly if 'Americas' is passed as the first argument, but will return undefined if getUserImputForm() is passed.
【问题讨论】:
-
getUserInputForm不返回任何内容,它只是添加了一个事件监听器。 -
非常感谢,现在一切都整理好了。我应该在 addEventListener 函数中调用 match 函数,并将来自它的值作为第一个 arg 传递。
标签: javascript function parameters return undefined