gpt4 book ai didi

javascript - 使用 Javascript 将 Bootstrap 下拉列表连接到对象并循环

转载 作者:行者123 更新时间:2023-12-03 05:15:13 26 4
gpt4 key购买 nike

这次我尝试编写一个代码,让用户选择他/她想去的位置。我已经编写了 html 和 js,但现在我被连接它们所困扰(大约半天......)。我在 Bootstrap 的帮助下编写了一个下拉菜单。我还编写了一个 js 文件,它应该循环这些位置。最后它应该理解用户选择了什么,然后输出结果应该是:

var text = '<p>' + buses[i].to + '<br> - Distance from Estonia: ' + buses[i].distance + ' km <br> - Price: ' + buses[i].price + ' euros <br>'

在网络浏览器中是这样的:

阿姆斯特丹

  • 距离爱沙尼亚:2000公里
  • 价格:250 欧元

但是无论我尝试什么,都没有效果(我知道这是因为我知识匮乏,我不怪JS)。我尝试过将“id”和“onclick 函数”放在任何地方。

所以,我将粘贴其中的一小部分,也许有人可以提供帮助:)

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>

<h2>Buses</h2>

<p id="test1">...</p>
<p id="test2">...</p>

<script src="js/buses.js"></script>


<!-- Dropdown menu for beautiful usage -->

<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" id="dropdownMenu2" type="button">
List of locations
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu2">
<ul id="boss">
<li><a role="button" onclick="bus()" value="Amsterdam">">Amsterdam</a></li>
<li><a role="button" onclick="bus()" value="Madrid">Madrid</a></li>
<li><a role="button" onclick="bus()" value="Stockholm">Stockholm</a></li>
<li><a role="button" onclick="bus()" value="Riga">Riga</a></li>
</ul>
</div>
</div>

</body>
</html>

还有我的 JS 文件:

// If person is in some location

document.getElementById("test2").innerHTML = "<hr> You're in Estonia, where would You want to travel?";

function bus() {

// Object with info
var buses = [
{ to: 'Amsterdam', distance: 2000, price: 250},
{ to: 'Madrid', distance: 4500, price: 350},
{ to: 'Stockholm', distance: 1500, price: 90},
{ to: 'Riga', distance: 400, price: 100}
];

// Should get result which user has choosen
var res = document.getElementById("boss");
var result = res.value;
console.log(value); // outputs html row... >.>

// Loop through the object
var text = "";
for(var i = 0; i < (buses.length); i++) {

if (result == buses[i].to) { text = '<p>' + buses[i].to + '<br> - Distance from Estonia: ' + buses[i].distance + ' km <br> - Price: ' + buses[i].price + ' euros <br>';
alert("Works!!!!"); // Well, last thing is to change it to output "var text" result
}

};
document.getElementById("test2").innerHTML = text; // If everything is correct, this should output sentence about selected bus
}

谢谢!

最佳答案

在 onclick 函数本身中传递城市的值

<li><a role="button" onclick="bus('Amsterdam')">Amsterdam</a></li>

并在函数中像这样接收它

function bus(value) {
var result=value;
}

关于javascript - 使用 Javascript 将 Bootstrap 下拉列表连接到对象并循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41635961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com