【发布时间】:2020-12-05 18:33:20
【问题描述】:
我是编写 JS 函数的新手,正在努力使以下用例工作:
- 根据 HTML 页面中下拉列表的值,更改 lodash 函数中的变量并将其提交以在 HTML div 中显示。
当我将变量 countryA 和 countryB 从更广泛的函数中拉出时,我似乎无法从以下得到任何空白输出,然后它可以工作,但在里面时却不行。
非常感谢任何帮助:
HTML
<!DOCTYPE html>
<head>
</head>
<body>
<h2>Auto populated dropdown</h2>
<form id="myForm">
<select id="selectCountry1">
</select>
<select id="selectCountry2">
</select>
<input id="searchCountry" type="button" value="Take me travelling"/>
</form>
<h2>Result</h2>
<div id="travelResult"></div>
</body>
</html>
JS
const UK = [
{id:'GB', name:'Great Britain', QT:'14 Day', info:'visit gov.uk'},
{id:'CR', name:'Croatia', QT:'0 Day', info:'visit gov.uk/'},
{id:'EU', name:'European Union', QT:'0 Day', info:'visit gov.uk/'},
{id:'IRL', name:'Ireland', QT:'0 Day', info:'visit gov.uk/'}
];
const USA =[
{id:'USA', name:'USA', QT:'14 Day', info:'visit gov.uk/'},
{id:'CR', name:'Croatia', QT:'14 Day', info:'visit us.gov.croatia'},
{id:'IRL', name:'Ireland', QT:'0 Day', info:'visit gov.uk/'}
];
const EU =[
{id:'GB', name:'Great Britain', QT:'14 Day', info:'visit gov.uk/'},
{id:'CR', name:'Croatia', QT:'14 Day', info:'visit us.gov.croatia'},
{id:'IRL', name:'Ireland', QT:'0 Day', info:'visit gov.uk/'},
{id:'EU', name:'European Union', QT:'0 Day', info:'visit gov.uk/'},
];
const IRL =[
{id:'GB', name:'Great Britain', QT:'14 Day', info:'visit gov.uk/'},
{id:'EU', name:'European Union', QT:'0 Day', info:'visit gov.uk/'},
{id:'IRL', name:'Ireland', QT:'0 Day', info:'visit gov.uk/'}
];
const options = ["UK", "USA", "EU", "IRL"];
// Function to build a dropdown
var select = document.getElementById("selectCountry1");
for(var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
};
var select = document.getElementById("selectCountry2");
for(var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
};
// var countryA = UK;
// var countryB = USA;
// Function to compare everything
window.onload = function(){
document.getElementById("searchCountry").onclick = resultSection;
};
function resultSection() {
var e = document.getElementById("selectCountry1");
var countryA = e.value;
console.log(countryA);
var f = document.getElementById("selectCountry2");
var countryB = f.value;
console.log(countryB);
var result6 = _.intersectionBy(countryA, countryB, 'id');
document.getElementById('travelResult').innerHTML = JSON.stringify(result6);
console.log(result6)
};
编辑:更新:通过在返回时手动定义每个变量来使其工作,可能有更好的方法来做到这一点,但它可以工作!
if (countryA === "UK"){
var firstCountry = UK
} else if (countryA === "USA") {
var firstCountry = USA
} else if (countryA === "EU") {
var firstCountry = EU
} else if (countryA === "IRL") {
var firstCountry = IRL
} else {
var firstCountry = "Error"
};
if (countryB === "UK"){
var secondCountry = UK
} else if (countryB === "USA") {
var secondCountry = USA
} else if (countryB === "EU") {
var secondCountry = EU
} else if (countryB === "IRL") {
var secondCountry = IRL
} else {
var secondCountry = "Error"
};
【问题讨论】:
标签: javascript html arrays json lodash