JavaScript表单出现问题

时间:2020-07-26 14:49:34

标签: javascript html forms

我是Java的新手,我正在研究这种形式。目标是让用户输入有关其房屋的信息,然后将输出预测价格。不幸的是,无论出于何种原因,我的按钮都无法使用,因此没有任何输出。我想知道我是否正确设置了表格。

<html> 
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title> </title>
        <!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]--> 
        <link rel="stylesheet" type="text/css" href="css/reset.css">
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <link href='http://fonts.googleapis.com/css?family=Crete+Round' rel='stylesheet' type='text/css'>
        <style type="text/css">
        </style>
        <meta charset="UTF-8" />
        <script type="text/javascript">
            function getPrice() {
                var form = document.getElementById("calc");
                var out = form.elements["z"];
                //get numbers
                var sqftVal = parseInt(form.elements["sqft"].value);
                var bathsVal = parseFloat(form.elements["baths"].value);
                var builtVal = parseInt(form.elements["built"].value);
                var lotVal = parseFloat(form.elements["lot"].value);
                
                for (i = 0; i < document.forms[0].gar.length; i++) {
                    if (document.forms[0].gar[i].checked) {
                        var garageVal = parseInt(document.forms[0].gar[i].value);
                    }
                }
                for (i = 0; i < document.forms[0].housetype.length; i++) {
                    if (document.forms[0].housetype[i].checked) {
                        var houseVal = document.forms[0].housetype[i].value;
                    }
                }
                for (i = 0; i < document.forms[0].neigh.length; i++) {
                    if (document.forms[0].neigh[i].checked) {
                        var neighVal = document.forms[0].neigh[i].value;
                    }
                }
                
                
                tempNum = -4309000.0 
                + 215200 * Math.log(sqftVal) 
                + 162300 * logVal
                + 526.2 * Math.exp(bathsVal)
                + 1576 * builtVal;
                
                if (garageVal == "2") {
                    tempNum = tempNum + 55830;
                }
                else if (garageVal == "3") {
                    tempNum = tempNum + 105000;
                }
                
                if (houseVal == "tw") {
                    tempNum = tempNum - 64780;
                }
                
                if (neighVal == "w") {
                    tempNum = tempNum + 102000;
                }
                else if (neighVal == "o") {
                    tempNum = tempNum + 40210;
                }
                
                out.value = Math.round(tempNum);
                
                
            }   
        </script>
    </head>
    <body>
        <form>
        <div id="featuresAshburn">
            <h3>Find Out What Your Home is Worth</h3>
            <font color = "#A52A2A"> <center> Note: For condominiums, be sure to input a lot size of 0. For townhomes, be sure to input the <br> appropriate lot size. Do not leave any question blank in order to receive an accurate prediction. </center> </font>
            <div class="wrapper">
                <ul>
                    <li class="feature-1">
                        <p> </p>
                        <form id="calc">
                            <fieldset>
                                <legend>House Type</legend>
                                <div>
                                    <input type="radio" id="sf" name="housetype" value="sf" checked="checked" />
                                    <label for="sf">Single Family</label>
                                </div>
                                <div>
                                    <input type="radio" id="tw" name="housetype" value="tw" />
                                    <label for="tw">Townhouse/Condo</label>
                                </div>
                            </fieldset>
                            <p> </p>
                            <fieldset>
                                <legend>Neighborhood</legend>
                                <div>
                                    <input type="radio" id="o" name="neigh" value="o"  />
                                    <label for="o">Oak Hill</label>
                                </div>
                                <div>
                                    <input type="radio" id="w" name="neigh" value="w" />
                                    <label for="w">Willow</label>
                                </div>
                                <div>
                                    <input type="radio" id="other" name="neigh" value="other" checked="checked" />
                                    <label for="other">Other</label>
                                </div>
                            </fieldset>
                        </li>
                        <li class="feature-2">
                            <p> </p>
                            <fieldset>
                                <legend>Square Feet</legend>
                                <input name="sqft" id="sqft" type="number" min="0" max="8000" value = "2500" required />
                            </fieldset>
                            <fieldset>
                                <legend>Number of Bathrooms</legend>
                                <input
                                name="baths"
                                id="baths"
                                type="number"
                                step="0.5"
                                min="0.5"
                                max="6.5"
                                value = "3.5"
                                required
                                />
                            </fieldset>
                            <fieldset>
                                <legend>Year Built</legend>
                                <input name="built" id="built" type="number" min="1988" max="2020" value = "2004" required />
                            </fieldset>
                            <fieldset>
                                <legend>Lot Size (in Acres)</legend>
                                <input
                                name="lot"
                                id="lot"
                                type="number"
                                step="0.01"
                                min="0"
                                max="1.54"
                                value = "0.11"
                                required
                                />
                            </fieldset>
                            <p> </p>
                            <!-- Add this element -->
                            <fieldset>
                                <legend>Predicted value</legend>
                                <input name="z" />
                            </fieldset>
                            <button type="button" onclick="getPrice()">
                                Predict My Home's Value
                            </button>     
                        </li>
                        <li class="feature-3">
                            <p> </p>
                            <fieldset>
                                <legend>Number of Garages</legend>
                                <div>
                                    <input type="radio" id="0" name="gar" value="0" checked="checked" />
                                    <label for="a">0</label>
                                </div>
                                <div>
                                    <input type="radio" id="1" name="gar" value="1" />
                                    <label for="b">1</label>
                                </div>
                                <div>
                                    <input type="radio" id="2" name="gar" value="2" />
                                    <label for="c">2</label>
                                </div>
                                <div>
                                    <input type="radio" id="3" name="gar" value="3" />
                                    <label for="d">3</label>
                                </div>
                            </fieldset>
                        </li>
                        <div class="clear"></div>
                    </ul>
                </div>
            </div>
        </form>
    </body>
</html>

与包装器没有任何关系,因为它们可以正常工作并显示得很好。我在其他城镇都有类似的其他页面,它们运行良好。

当我在编辑器中的堆栈溢出上运行它时,这是一个错误:

{
  "message": "Script error.",
  "filename": "",
  "lineno": 0,
  "colno": 0
}

不确定这是什么意思。单击按钮后,我得到了。

1 个答案:

答案 0 :(得分:2)

您遇到了一些小的html问题(例如form标记的位置),并且拼写了lotval logval

检查HTML代码的一种好方法是使用w3验证程序。另外,如果您检查console.log,您还会发现logVal未定义

      function getPrice() {
        var form = document.getElementById("calc");
        var out = form.elements["z"];
        //get numbers
        var sqftVal = parseInt(form.elements["sqft"].value);
        var bathsVal = parseFloat(form.elements["baths"].value);
        var builtVal = parseInt(form.elements["built"].value);
        var lotVal = parseFloat(form.elements["lot"].value);

        for (i = 0; i < document.forms[0].gar.length; i++) {
          if (document.forms[0].gar[i].checked) {
            var garageVal = parseInt(document.forms[0].gar[i].value);
          }
        }
        for (i = 0; i < document.forms[0].housetype.length; i++) {
          if (document.forms[0].housetype[i].checked) {
            var houseVal = document.forms[0].housetype[i].value;
          }
        }
    for (i = 0; i < document.forms[0].neigh.length; i++) {
          if (document.forms[0].neigh[i].checked) {
            var neighVal = document.forms[0].neigh[i].value;
          }
        }
          
          
            tempNum = -4309000.0 
            + 215200 * Math.log(sqftVal) 
            + 162300 * lotVal
                + 526.2 * Math.exp(bathsVal)
                + 1576 * builtVal;
          
          if (garageVal == "2") {
              tempNum = tempNum + 55830;
          }
          else if (garageVal == "3") {
              tempNum = tempNum + 105000;
          }
          
          if (houseVal == "tw") {
              tempNum = tempNum - 64780;
          }
          
          if (neighVal == "w") {
              tempNum = tempNum + 102000;
          }
          else if (neighVal == "o") {
              tempNum = tempNum + 40210;
          }
          
          out.value = Math.round(tempNum);
            
          
      }
    
    
    
<html> 
      
     
 <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     <title> </title>
     <!--[if lt IE 9]>
     <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
     <![endif]--> 
     <link rel="stylesheet" type="text/css" href="css/reset.css">
     <link rel="stylesheet" type="text/css" href="css/style.css">
     <link href='http://fonts.googleapis.com/css?family=Crete+Round' rel='stylesheet' type='text/css'>

 <style type="text/css">
</style>

<meta charset="UTF-8" />

  </head>
  <body>


    

<div id="featuresAshburn">
        <h3>Find Out What Your Home is Worth</h3>
        <font color = "#A52A2A"> <center> Note: For condominiums, be sure to input a lot size of 0. For townhomes, be sure to input the <br> appropriate lot size. Do not leave any question blank in order to receive an accurate prediction. </center> </font>
        <div class="wrapper">
         <form id="calc">

        <ul>
            <li class="feature-1">
                <p> </p>
        

           
                  <fieldset>
                <legend>House Type</legend>
                <div>
                  <input type="radio" id="sf" name="housetype" value="sf" checked="checked" />
                  <label for="sf">Single Family</label>
                </div>
                <div>
                  <input type="radio" id="tw" name="housetype" value="tw" />
                  <label for="tw">Townhouse/Condo</label>
                </div>
                  </fieldset>
                <p> </p>
                <fieldset>
                <legend>Neighborhood</legend>
                <div>
                  <input type="radio" id="o" name="neigh" value="o"  />
                  <label for="o">Oak Hill</label>
                </div>
                <div>
                  <input type="radio" id="w" name="neigh" value="w" />
                  <label for="w">Willow</label>
                </div>
                <div>
                  <input type="radio" id="other" name="neigh" value="other" checked="checked" />
                  <label for="other">Other</label>
                </div>
                  </fieldset>
                
            </li>
            <li class="feature-2">
                <p> </p>
        

            <fieldset>
                <legend>Square Feet</legend>
                <input name="sqft" id="sqft" type="number" min="0" max="8000" value = "2500" required />
                  </fieldset>
                  <fieldset>
                <legend>Number of Bathrooms</legend>
                <input
                  name="baths"
                  id="baths"
                  type="number"
                  step="0.5"
                  min="0.5"
                  max="6.5"
                       value = "3.5"
                       required
                />
                  </fieldset>
                  <fieldset>
                <legend>Year Built</legend>
                <input name="built" id="built" type="number" min="1988" max="2020" value = "2004" required />
                  </fieldset>
                  <fieldset>
                <legend>Lot Size (in Acres)</legend>
                <input
                  name="lot"
                  id="lot"
                  type="number"
                  step="0.01"
                  min="0"
                  max="1.54"
                       value = "0.11"
                       required
                />
                  </fieldset>
                
                <p> </p>
            
                
                <!-- Add this element -->
      <fieldset>
        <legend>Predicted value</legend>
        <input name="z" />
      </fieldset>
                
    <button type="button" onclick="getPrice()">
        Predict My Home's Value
      </button>       

            
            </li>
            <li class="feature-3">
                <p> </p>

            
            <fieldset>
                <legend>Number of Garages</legend>
                <div>
                  <input type="radio" id="0" name="gar" value="0" checked="checked" />
                  <label for="a">0</label>
                </div>
                <div>
                  <input type="radio" id="1" name="gar" value="1" />
                  <label for="b">1</label>
                </div>
                <div>
                  <input type="radio" id="2" name="gar" value="2" />
                  <label for="c">2</label>
                </div>
                <div>
                  <input type="radio" id="3" name="gar" value="3" />
                  <label for="d">3</label>
                </div>
                  </fieldset>
                
            </li>
            <div class="clear"></div>
        </ul>
        
   
      
      
    </form>