gpt4 book ai didi

javascript - 在输入文本js上显示数组结果

转载 作者:行者123 更新时间:2023-12-03 12:12:16 25 4
gpt4 key购买 nike

我试图在价格输入中显示每件商品的价格,但我无法使该功能正常工作。我不确定如何写入字符串,或者我是否正确执行该功能。你能帮我么?谢谢!

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Price Calculation</title>
</head>

<body>
<Label>Choose Item</Label>
<select onChange=".......">
<option>Tomatoes</option>
<option>Lettuce</option>
<option>Potato</option>
<option>Carrots</option>
<option>Artichoke</option>
</select>
<br/>
<Label>Price</Label>
<input type="text" id="price" />
<br/>
<script type="text/javascript">
var veggy = new Array ()
veggy ["Tomatoes"] = 5.99
veggy ["Lettuce"] = 7.66
veggy ["Potato"] = 4.52
veggy ["Carrots"] = 2.13
veggy ["Artichoke"] = 10.58

function ()
{
var veggy = document.getElementById("price")
}
</script>
</body>
</html>

最佳答案

这里需要更改一些内容:

您需要一个对象,其中每个选择作为键,每个价格作为值 - 而不是数组调用函数 onchange 并通过 this 传入 select 元素

//Make veggy an object
var veggy = {};
veggy["Tomatoes"] = 5.99;
veggy["Lettuce"] = 7.66;
veggy["Potato"] = 4.52;
veggy["Carrots"] = 2.13;
veggy["Artichoke"] = 10.58;

更改您的选择 onChange:

<select onChange="getPrice(this);">

你的函数:

function getPrice(select) {
document.getElementById("price").value = veggy[select.value];
}

关于javascript - 在输入文本js上显示数组结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24911103/

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