Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将输入上显示的两个变量(JS)相乘_Javascript - Fatal编程技术网

Javascript 将输入上显示的两个变量(JS)相乘

Javascript 将输入上显示的两个变量(JS)相乘,javascript,Javascript,我有一个练习,我需要显示项目、价格、数量价格等。。。我很难把价格乘以数量。下面是我的代码以及我正在显示的输入和选项,以及下面的脚本函数,非常感谢您的帮助!: <body> <label>Choose Item</label> <select onChange="getPrice(this)"> <option></option> <option>Tomatoes</option> <

我有一个练习,我需要显示项目、价格、数量价格等。。。我很难把价格乘以数量。下面是我的代码以及我正在显示的输入和选项,以及下面的脚本函数,非常感谢您的帮助!:

<body>
<label>Choose Item</label>
<select onChange="getPrice(this)">
  <option></option>
  <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/>
<label>Quantity</label>
<select onChange="getFull(this)">
  <option></option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>
<br />
<label>Total $</label>
  <input type="text" id="quantityPrice" />

<script type="text/javascript">
  var veggy = {};
  veggy ["Tomatoes"] = 5.99 
  veggy ["Lettuce"] = 7.66 
  veggy ["Potato"] = 4.52 
  veggy ["Carrots"] = 2.13 
  veggy ["Artichoke"] = 10.58

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

var quantity = {};
  quantity ["1"] = 1 
  quantity ["2"] = 2
  quantity ["3"] = 3
  quantity ["4"] = 4
  quantity ["5"] = 5

function getFull(select) {
    document.getElementById("quantityPrice").value = (quantity[select.value] * veggy [select.value]);
}
</script>
</body>
</html>

将数字转换为值中的字符串

document.getElementById("quantityPrice").value =''+ (quantity[select.value] * veggy [select.value]);

问题是,您正试图使用price select键从vegy对象获取值

这里您传递的是Quantity select的引用,但您必须获得price form Item对象

只需为您的第一个select for ChooseItems提供一个Id

<select id="veggy" onChange="getPrice(this)">

我添加了一个答案,试试看。它很有效!非常感谢你!我不知道为什么您添加了var veg select…@Yaelix我正在获取Items select框以获取该变量的选定值
<select id="veggy" onChange="getPrice(this)">
function getFull(select) {
    var vegSelect = document.getElementById('veggy');
    document.getElementById("quantityPrice").value = (quantity[select.value] * (veggy[vegSelect.value]));
}