2017-11-19 14 views
0

このエラーが発生するUncaught ReferenceError:calculatePriceがHTMLInputElement.onclickで定義されていません。未捕捉ReferenceErrorを取得する

私は既にこのエラーに関する他の回答を見ており、どれもうまくいきませんでした。私は体や頭でスクリプトを定義しようとしましたが、違いはありませんでした。私はまた、私はソリューションに比べて、私はそれを接続する機能とボタンが正しいと思うので、私はそれがなぜそれが私がそれを押して動作していないのか分からない。

<html> 
    <head> 
     <link href="style/myStyles.css" type="text/css" rel="stylesheet"> 
      <title> The Printing Company </title> 
    </head> 
    <body> 
    <script type="text/javascript"></script> 
     <script> 
     function calculatePrice() { 
      var quantity, type, price 
      quantity = document.getElementById("qty").value; 
      type = document.getElementById("cardstype").value; 
      if (type.includes("Basic"){ 
       price = 10*(quantity/100); 
      } else if (type.includes("Medium"){ 
       price = 15*(quantity/100); 
      } else if (type.includes("High"){ 
       price = 20*(quantity/100); 
      } 
      alert("Your order will cost " + price + " GBP."); 
      } 
     </script> 

     <form action="form.php" method="post"> 
       <label >Company Name</label><br> 
       <input name="name" type="text" required /><br> 
       <br> 
       <label>Contact email</label><br> 
       <input name="email" type="email" required /><br> 
       <br> 
       <label>Business Card type</label><br> 
       <select name="cardstype" id="cardstype"> 
        <option value="" disabled selected>-Select Card Quality-</option> 
        <option value="Basic Quality">Basic Quality</option> 
        <option value="Medium Quality">Medium Quality</option> 
        <option value="High Quality">High Quality</option> 
       </select><br> 
       <br> 
       <label>Business Cards quantity</label><br> 
       <input name="qty" id ="qty" type="number" required Onchange = ' 
         if(!(this.value >= 100 && this.value <= 1000)) { 
          //alert the user that they have made a mistake 
          alert(this.value + " is not a valid quantity. Minimun quantity per order: 100 and maximum: 1000."); 
          this.value=""; //clears the text box 
         }'><br> 
       <br> 
       <input type="button" name="Price" value="Calculate Price" onclick="calculatePrice()"/> 
       <input type="submit" value="Submit Order"/> 
      </form> 
    </html> 

誰でも手助けできますか?

+0

投票:

if (type.includes("Basic") { ^^^ here 

は例の作業typoは他のユーザーに役立つものではありません。 –

+0

こんにちは、私はスペルの間違いを修正しましたが、何も修正しませんでした –

+0

ああ、実際には、私はそれらを指摘するために私の答えを編集します。 –

答えて

0

文場合、私はあなたの中に閉じ括弧が欠落していると思う:シンプルな疑問として、これを閉じるために

function calculatePrice() { 
 

 
    var quantity, type, price; 
 
    quantity = document.getElementById("qty").value; 
 
    type = document.getElementById("cardstype").value; 
 

 
    if (type.includes("Basic")) { 
 
     price = 10*(quantity/100); 
 
    } else if (type.includes("Medium")) { 
 
     price = 15*(quantity/100); 
 
    } else if (type.includes("High")) { 
 
     price = 20*(quantity/100); 
 
    } 
 

 
    alert("Your order will cost " + price + " GBP."); 
 

 
}
<form action="form.php" method="post"> 
 
      <label >Company Name</label><br> 
 
      <input name="name" type="text" required /><br> 
 
      <br> 
 
      <label>Contact email</label><br> 
 
      <input name="email" type="email" required /><br> 
 
      <br> 
 
      <label>Business Card type</label><br> 
 
      <select name="cardstype" id="cardstype"> 
 
       <option value="" disabled selected>-Select Card Quality-</option> 
 
       <option value="Basic Quality">Basic Quality</option> 
 
       <option value="Medium Quality">Medium Quality</option> 
 
       <option value="High Quality">High Quality</option> 
 
      </select><br> 
 
      <br> 
 
      <label>Business Cards quantity</label><br> 
 
      <input name="qty" id ="qty" type="number" required Onchange = ' 
 
        if(!(this.value >= 100 && this.value <= 1000)) { 
 
         //alert the user that they have made a mistake 
 
         alert(this.value + " is not a valid quantity. Minimun quantity per order: 100 and maximum: 1000."); 
 
         this.value=""; //clears the text box 
 
        }'><br> 
 
      <br> 
 
      <input type="button" name="Price" value="Calculate Price" onclick="calculatePrice(event)"/> 
 
      <input type="submit" value="Submit Order"/> 
 
     </form>

+0

大変ありがとうございます。私の愚かな間違いを編集してください。 –

+0

@ Codenoby喜んで助けてください:) – YouneL

+0

うん、あなたは大丈夫ですか? –

0

あなたの機能のいくつかのタイプミスがあります:右中括弧がありません

  • (ルックスは今固定)変数定義の後
  • セミコロン:var quantity, type, price;
  • 不一致括弧をIF/ELSEIF条件に(3回):if (type.includes("Basic")) {

次のコードは、これらの問題は、固定されています:

<script> 
function calculatePrice() { 
    var quantity, type, price; 
    quantity = document.getElementById("qty").value; 
    type = document.getElementById("cardstype").value; 
    if (type.includes("Basic")){ 
     price = 10*(quantity/100); 
    } else if (type.includes("Medium")){ 
     price = 15*(quantity/100); 
    } else if (type.includes("High")){ 
     price = 20*(quantity/100); 
    } 
    alert("Your order will cost " + price + " GBP."); 
} 
</script> 

したがって、calculatePrice関数は定義されておらず、呼び出すこともできません。

+0

こんにちは、私はスペルミスを修正しましたが、何も修正しませんでした –

+0

私の編集を確認してください:) –

+0

セミコロンは現在javacriptでオプションです – YouneL

関連する問題