2016-05-24 11 views
-4

私は見積もりの​​ためにキャッチしていないタイプのエラーを取得しています。また、配偶者との価格を倍にする方法にも問題があります。都市からの選択、日数、その他のオプションに基づいて見積もりコストを表示するアラートウィンドウがあるとします。 TY! onClick="estimateCost()"コードでボタン入力にJavascriptフォームの計算エラー

<html> 
 

 
<head> 
 

 
    <script language="JavaScript"> 
 
    var city_prices = new Array(); 
 
    city_prices["New York City"] = 0; 
 
    city_prices["Boston"] = 0; 
 
    city_prices["San Francisco"] = 200; 
 
    city_prices["Los Angeles"] = 200; 
 

 
    // getCityPrice() finds the price based on the city . 
 
    // Here, we need to take user's the selection from radio button selection 
 
    function getCityPrice() { 
 
     var cityPrice = 0; 
 
     //Get a reference to the form id="cakeform" 
 
     var theForm = document.forms["form1"]; 
 
     //Get a reference to the cake the user Chooses name=radCity": 
 
     var selectedCity = theForm.elements["radCity"]; 
 
     //Here since there are 4 radio buttons selectedCity.length = 4 
 
     //We loop through each radio buttons 
 
     for (var i = 0; i < selectedCity.length; i++) { 
 
     //if the radio button is checked 
 
     if (selectedCity[i].checked) { 
 
      //we set cityPrice to the value of the selected radio button 
 
      //i.e. if the user choose NYC we set to 0 
 
      //by using the city_prices array 
 
      //We get the selected Items value 
 
      //For example city_prices["New York City".value]" 
 
      cityPrice = city_prices[selectedCity[i].value]; 
 
      //If we get a match then we break out of this loop 
 
      //No reason to continue if we get a match 
 
      break; 
 
     } 
 
     } 
 
     //We return the cityPrice 
 
     return cityPrice; 
 
    } 
 

 
    var number_days = new Array(); 
 
    number_days["3"] = 450; 
 
    number_days["4"] = 600; 
 
    number_days["5"] = 750; 
 
    number_days["6"] = 900; 
 
    number_days["7"] = 1050; 
 
    number_days["8"] = 1350; 
 
    number_days["9"] = 1500; 
 
    number_days["10"] = 1650; 
 
    number_days["11"] = 1800; 
 
    number_days["12"] = 1950; 
 
    number_days["13"] = 2100; 
 
    number_days["14"] = 2250; 
 
    number_days["15"] = 2400; 
 

 
    //This function finds the day price based on the 
 
    //drop down selection 
 
    function getDayPrice() { 
 
     var dayPrice = 0; 
 
     //Get a reference to the form id="form1" 
 
     var theForm = document.forms["form1"]; 
 
     //Get a reference to the select id="selNumberDays" 
 
     var selectedDays = theForm.elements["selNumberDays"]; 
 

 
     //set dayPrice equal to value user chose 
 
     //For example number_days["3".value] would be equal to 450 
 
     dayPrice = number_days[selectedDays.value]; 
 

 
     //finally we return dayPrice 
 
     return dayPrice; 
 
    } 
 

 

 
    //chksFirst() finds the candles price based on a check box selection 
 
    function chksFirst() { 
 
     var chkFirst = 0; 
 
     //Get a reference to the form id="form1" 
 
     var theForm = document.forms["form1"]; 
 
     //Get a reference to the checkbox id="chkFirst" 
 
     var includeFirst = theForm.elements["chkFirst"]; 
 

 
     //If they checked the box set first class to 500 
 
     if (includeFirst.checked == true) { 
 
     chkFirst = 500; 
 
     } 
 
     //finally we return the firstClass 
 
     return chkFirst; 
 
    } 
 

 
    //chksSpouse() finds the candles price based on a check box selection 
 
    function chksSpouse() { 
 
     var chkSpouse = 0; 
 
     //Get a reference to the form id="form1" 
 
     var theForm = document.forms["form1"]; 
 
     //Get a reference to the checkbox id="chkSpouse" 
 
     var includeSpouse = theForm.elements["chkSpouse"]; 
 

 
     //If they checked the box set Total 2x 
 
     if (includeSpouse.checked == true) { 
 
     totalPrice = totalPrice * 2; 
 
     } 
 
     //finally we return the firstClass 
 
     return totalPrice; 
 
    } 
 

 

 
    function estimateCost() { 
 
     //Here we get the estimate price by calling our function 
 
     //Each function returns a number so by calling them we add the values they return together 
 
     var totalPrice = getCityPrice() + getDayPrice() + 
 
     chksFirst() + chksSpouse(); 
 

 
     alert(totalPrice); 
 
    } 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <table align="left" width="600px" border="0" cellpadding="5px"> 
 
    <tr> 
 
     <td> 
 
     <form name="form1" id="form1"> 
 
      <table border="0"> 
 
      <tr> 
 
       <td width="300px"><strong>Last Name: </strong> 
 
       </td> 
 
       <td width="300px"> 
 
       <input type="text" name="txtFirstName" value=" " size="20" /> 
 
       </td> 
 
      </tr> 
 

 
      <tr> 
 
       <td><strong>First Name: </strong> 
 
       </td> 
 
       <td> 
 
       <input type="text" name="txtLastName" value=" " size="20" /> 
 
       </td> 
 
      </tr> 
 

 
      <tr> 
 
       <td><strong>Nationality: </strong> 
 
       </td> 
 
       <td> 
 
       <select name="selNationality"> 
 
        <option value="amer">USA</option> 
 
        <option value="can">Canada</option> 
 
        <option value="mex">Mexico</option> 
 
        <option value="ger">Germany</option> 
 
        <option value="fra">France</option> 
 
       </select> 
 
       </td> 
 
      </tr> 
 

 
      <tr> 
 
       <td><strong>City you wish to visit: </strong> 
 
       </td> 
 
       <td> 
 
       <input type="radio" name="radCity" value="New York City" />New York City 
 
       <br /> 
 
       <input type="radio" name="radCity" value="Boston" />Boston 
 
       <br /> 
 
       <input type="radio" name="radCity" value="San Francisco" />San Francisco ($200 surcharge) 
 
       <br /> 
 
       <input type="radio" name="radCity" value="Los Angeles" />Los Angeles ($200 surcharge) 
 
       <br/> 
 
       </td> 
 
      </tr> 
 

 
      <tr> 
 
       <td><strong>Number of days ($150 per day): </strong> 
 
       </td> 
 
       <td> 
 
       <select name="selNumberDays" id="selNumberDays"> 
 
        <option value="3">3</option> 
 
        <option value="4">4</option> 
 
        <option value="5">5</option> 
 
        <option value="6">6</option> 
 
        <option value="7">7</option> 
 
        <option value="8">8</option> 
 
        <option value="9">9</option> 
 
        <option value="10">10</option> 
 
        <option value="11">11</option> 
 
        <option value="12">12</option> 
 
        <option value="13">13</option> 
 
        <option value="14">14</option> 
 
        <option value="15">15</option> 
 
       </td> 
 
      </tr> 
 

 
      <tr> 
 
       <td><strong>Other options: </strong> 
 
       </td> 
 
       <td> 
 
       <input type="checkbox" name="chkFirst" id="chkFirst" />First Class Only ($500 surcharge) 
 
       <br /> 
 
       <input type="checkbox" name="chkSpouse" id="chkSpouse" />Traveling with Spouse (All costs doubled) 
 
       <br /> 
 
       </td> 
 
      </tr> 
 
      <tr> 
 
       <td align="right"> 
 
       <input type="button" value="Give me an estimate!" onClick="estimateCost()" id="estimateCost" /> 
 
       </td> 
 
       <td align="left"> 
 
       <input type="reset" /> 
 
       </td> 
 
      </tr> 
 
      </table> 
 
     </form> 
 
     </td> 
 
    </tr> 
 

 
    </table> 
 
</body> 
 

 
</html>

答えて

1

id="estimateCost"を取り除きます。何らかの理由でエラーが発生しています。あなたは本当にonclickの代わりに、インラインのかかわらのonClickリスナを使用する必要があります。 Inline onclick JavaScript variable

配偶者との合計について、あなたはchksSpouse関数に前配偶者の価格を渡し、このような何かにそれを手直しすることができますし、合計価格としてのリターンを使用してください。

//chksSpouse() finds the candles price based on a check box selection 
function chksSpouse(totalPrice) { 
    var chkSpouse = 0; 
    //Get a reference to the form id="form1" 
    var theForm = document.forms["form1"]; 
    //Get a reference to the checkbox id="chkSpouse" 
    var includeSpouse = theForm.elements["chkSpouse"]; 

    //If they checked the box set Total 2x 
    if (includeSpouse.checked == true) { 
    totalPrice = totalPrice * 2; 
    } 
    //finally we return the firstClass 
    return totalPrice; 
} 

function estimateCost() { 
    //Here we get the estimate price by calling our function 
    //Each function returns a number so by calling them we add the values they return together 
    var preSpouseTotal = getCityPrice() + getDayPrice() + chksFirst(); 
    var totalPrice = chksSpouse(preSpouseTotal); 

    alert(totalPrice); 
} 
+0

これは、要素を参照するグローバル変数に要素IDを作るために非常に古いIEの設計上の特徴の結果である –

+0

ありがとうございました。当時のデザインの決定は非常に悪いものでしたが、他のブラウザでも互換性を実現していました。 – RobG

+0

ああ、ありがとう、ありがとう! – Anialation