首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的清单,汽车按产品数量计算价格,不会接受产品的数量

我的清单,汽车按产品数量计算价格,不会接受产品的数量
EN

Stack Overflow用户
提问于 2022-06-30 09:38:29
回答 3查看 27关注 0票数 0

我试图做一个列表,你可以把你想要的产品数量,按一个按钮,所有产品的和将被计算,我有困难的功能,以获得每种产品的安装。在尝试按钮时,我总是收到错误消息: Uncaught :document.getElementById(.)为空

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>
<head>

<script>
let WaschlotionAmount;
let HaarshampooAmont;
let BodylotionAmount;
let WundschutzcremeAmount;
let DeosprayAmount;
let ZahncremeAmount;
let ZahnbuersteAmount;
let ZahnspuellbecherAmount;
let ZahnhaftcremeAmount;
let ZahnreinigunstabsAmount;
let RasierklingenAmount;
let RasiererMitKlingenAmount;
let RasierschaumAmount;
let EinwegrasiererAmount;

let Sum;
</script>
</head>
<body>
  <table>
    <tr>
      <th>Kosmetikartikel</th>
      <th>Menge</th>
    </tr>
    <tr>
      <td>Waschlotion</td>
      <td><input id="WaschlotionAmount"></td>
    </tr>
    <tr>
      <td>Haarshampoo</td>
      <td><input id="HaarshampooAmount"></td>
    </tr>
    <tr>
      <td>Bodylotion</td>
      <td><input id="BodylotionAmount"></td>
    </tr>
    <tr>
      <td>Wundschutzcreme</td>
      <td><input id="WundschutzcremeAmount"></td>
    </tr>
    <tr>
      <td>Deospray</td>
     <td><input id="DeosprayAmount"></td>
   </tr>
   <tr>
    <td>Zahncreme</td>
    <td><input id="ZahncremeAmount"></td>
  </tr>
  <tr>
   <td>Zahnbürste</td>
   <td><input id="ZahnbuersteAmount"></td>
 </tr>
 <tr>
  <td>Zahnspüllbecher</td>
  <td><input id="ZahnspuellbecherAmount"></td>
</tr>
   <tr>
    <td>Zahnhaftcreme</td>
    <td><input id="ZahnhaftcremeAmount"></td>
  </tr>
  <tr>
    <td>Zahnreinigunstabs</td>
    <td><input id="ZahnreinigunstabsAmount"></td>
  </tr>
  <tr>
    <td>Rasierklingen</td>
    <td><input id="RasierklingenAmount"></td>
  </tr>
  <tr>
    <td>Rasierer mit Klingen</td>
    <td><input id="RasiererMitKlingenAmount"></td>
  </tr>
  <tr>
    <td>Rasierschaum</td>
    <td><input id="Rasierschaum"></td>
  </tr>
  <tr>
    <td>Einwegrasierer</td>
    <td><input id="EinwegrasiererAmount"></td>
  </tr>
  <tr>
    <td><b>Gesamtpreis</b></td>
    <td><p id="Sum"></p></td>
  </tr>
</table>
<button onclick="SetAmount()">Try me</button>
</body>
<script>
var Waschlotion = 1.30;
var Haarshampoo = 1.50;
var Bodylotion = 3;
var Wundschutzcreme = 4.80;
var Deospray = 1.80;
var Zahncreme = 3.20;
var Zahnbuerste = 1;
var Zahnspuellbecher = 1.25;
var Zahnhaftcreme = 1.50;
var Zahnreinigunstabs = 3.20;
var Rasierklingen = 2.49;
var RasiererMitKlingen = 4.50;
var Rasierschaum = 1.20;
var Einwegrasierer = 2.40;



function SetAmount() {
  let WaschlotionAmount = document.getElementById("WaschlotionAmount").value;
  let HaarshampooAmount = document.getElementById("HaarshampooAmount").value;
  let BodylotionAmount = document.getElementById("BodylotionAmount").value;
  let WundschutzcremeAmount = document.getElementById("WundschutzcremeAmount").value;
  let DeosprayAmount = document.getElementById("DeosprayAmount").value;
  let ZahncremeAmount = document.getElementById("ZahncremeAmount").value;
  let ZahnbuersteAmount = document.getElementById("ZahnbuersteAmount").value;
  let ZahnspuellbecherAmount = document.getElementById("ZahnspuellbecherAmount").value;
  let ZahnhaftcremeAmount = document.getElementById("ZahnhaftcremeAmount").value;
  let ZahnreinigunstabsAmount = document.getElementById("ZahnreinigunstabsAmount").value;
  let RasierklingenAmount = document.getElementById("RasierklingenAmount").value;
  let RasiererMitKlingenAmount = document.getElementById("RasiererMitKlingenAmount").value;
  let RasierschaumAmount = document.getElementById("RasierschaumAmount").value;
  let EinwegrasiererAmount = document.getElementById("EinwegrasiererAmount").value;
}

function Calculate() {
  document.getElementById("Sum").innerHTML = Sum;
}
 </script>
</html>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-06-30 09:43:50

RasierschaumAmount. Rasierschaum的id更改为看起来不错!

代码语言:javascript
运行
复制
 <!DOCTYPE html>
  <html>
    <body>
      <table>
        <tr>
          <th>Kosmetikartikel</th>
          <th>Menge</th>
        </tr>
        <tr>
          <td>Waschlotion</td>
          <td><input id="WaschlotionAmount"></td>
        </tr>
        <tr>
          <td>Haarshampoo</td>
          <td><input id="HaarshampooAmount"></td>
        </tr>
        <tr>
          <td>Bodylotion</td>
          <td><input id="BodylotionAmount"></td>
        </tr>
        <tr>
          <td>Wundschutzcreme</td>
          <td><input id="WundschutzcremeAmount"></td>
        </tr>
        <tr>
          <td>Deospray</td>
         <td><input id="DeosprayAmount"></td>
       </tr>
       <tr>
        <td>Zahncreme</td>
        <td><input id="ZahncremeAmount"></td>
      </tr>
      <tr>
       <td>Zahnbürste</td>
       <td><input id="ZahnbuersteAmount"></td>
     </tr>
     <tr>
      <td>Zahnspüllbecher</td>
      <td><input id="ZahnspuellbecherAmount"></td>
    </tr>
       <tr>
        <td>Zahnhaftcreme</td>
        <td><input id="ZahnhaftcremeAmount"></td>
      </tr>
      <tr>
        <td>Zahnreinigunstabs</td>
        <td><input id="ZahnreinigunstabsAmount"></td>
      </tr>
      <tr>
        <td>Rasierklingen</td>
        <td><input id="RasierklingenAmount"></td>
      </tr>
      <tr>
        <td>Rasierer mit Klingen</td>
        <td><input id="RasiererMitKlingenAmount"></td>
      </tr>
      <tr>
        <td>Rasierschaum</td>
        <td><input id="RasierschaumAmount"></td>
      </tr>
      <tr>
        <td>Einwegrasierer</td>
        <td><input id="EinwegrasiererAmount"></td>
      </tr>
      <tr>
        <td><b>Gesamtpreis</b></td>
        <td><p id="Sum"></p></td>
      </tr>
    </table>
    <button onclick="SetAmount()">Try me</button>
    </body>
    <script>
    let Waschlotion = 1.30;
    let Haarshampoo = 1.50;
    let Bodylotion = 3;
    let Wundschutzcreme = 4.80;
    let Deospray = 1.80;
    let Zahncreme = 3.20;
    let Zahnbuerste = 1;
    let Zahnspuellbecher = 1.25;
    let Zahnhaftcreme = 1.50;
    let Zahnreinigunstabs = 3.20;
    let Rasierklingen = 2.49;
    let RasiererMitKlingen = 4.50;
    let Rasierschaum = 1.20;
    let Einwegrasierer = 2.40;



    function SetAmount() {
      let WaschlotionAmount = document.getElementById("WaschlotionAmount").value;
      let HaarshampooAmount = document.getElementById("HaarshampooAmount").value;
      let BodylotionAmount = document.getElementById("BodylotionAmount").value;
      let WundschutzcremeAmount = document.getElementById("WundschutzcremeAmount").value;
      let DeosprayAmount = document.getElementById("DeosprayAmount").value;
      let ZahncremeAmount = document.getElementById("ZahncremeAmount").value;
      let ZahnbuersteAmount = document.getElementById("ZahnbuersteAmount").value;
      let ZahnspuellbecherAmount = document.getElementById("ZahnspuellbecherAmount").value;
      let ZahnhaftcremeAmount = document.getElementById("ZahnhaftcremeAmount").value;
      let ZahnreinigunstabsAmount = document.getElementById("ZahnreinigunstabsAmount").value;
      let RasierklingenAmount = document.getElementById("RasierklingenAmount").value;
      let RasiererMitKlingenAmount = document.getElementById("RasiererMitKlingenAmount").value;
      let RasierschaumAmount = document.getElementById("RasierschaumAmount").value;
      let EinwegrasiererAmount = document.getElementById("EinwegrasiererAmount").value;
    }

    function Calculate() {
      document.getElementById("Sum").innerHTML = Sum;
    }
     </script>
    </html>
票数 0
EN

Stack Overflow用户

发布于 2022-06-30 09:40:42

不要在SetAmount中用let重新声明变量。它遮挡了全局变量。

另外,您还声明了变量两次--一次以let开头,一次在中间使用var

票数 1
EN

Stack Overflow用户

发布于 2022-06-30 10:03:07

代码语言:javascript
运行
复制
function SetAmount() {
   WaschlotionAmount = document.getElementById("WaschlotionAmount").value;
   HaarshampooAmount = document.getElementById("HaarshampooAmount").value;
   BodylotionAmount = document.getElementById("BodylotionAmount").value;
   WundschutzcremeAmount = document.getElementById("WundschutzcremeAmount").value;
   DeosprayAmount = document.getElementById("DeosprayAmount").value;
   ZahncremeAmount = document.getElementById("ZahncremeAmount").value;
   ZahnbuersteAmount = document.getElementById("ZahnbuersteAmount").value;
   ZahnspuellbecherAmount = document.getElementById("ZahnspuellbecherAmount").value;
   ZahnhaftcremeAmount = document.getElementById("ZahnhaftcremeAmount").value;
   ZahnreinigunstabsAmount = document.getElementById("ZahnreinigunstabsAmount").value;
   RasierklingenAmount = document.getElementById("RasierklingenAmount").value;
   RasiererMitKlingenAmount = document.getElementById("RasiererMitKlingenAmount").value;
   RasierschaumAmount = document.getElementById("RasierschaumAmount").value;
   EinwegrasiererAmount = document.getElementById("EinwegrasiererAmount").value;
}

在您的函数中,您再次创建了一个仅在该函数中可用的变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72813516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档