未捕获的参考错误:HTMLButtonElement.onclick中未定义函数名称

时间:2017-12-01 11:52:52

标签: javascript function button onclick

这是我之前提问的编辑版本。 在这里,我想要一个ajax请求来获取类(数据库中的表的行名)。第一个函数工作正常,但第二个函数抛出此错误。

当我把这两个函数放在不同的函数中时,第一个工作和第二个抛出错误但是当我把两个函数放在同一个标​​签中时都会给出错误。 希望你明白。

代码是

function showoptionss(str) {
  if (str == "") {
    document.getElementById("options1").innerHTML = "";
    return;
  }
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("options1").innerHTML = this.responseText;
    }
  }
  xmlhttp.open("GET", "getdata.php?q=" + str, true);
  xmlhttp.send();
};

function gettotalcost() {
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var gottotalcost = this.responseText;
      alert(this.responseText);
      document.getElementById("total_cost").innerHTML = this.responseText;
    }
  }
  var itemtype = document.getElementById("itemtype").value;
  var options1 = document.getElementById("options1").value;
  var subs = document.getElementById("subs").value;
  xmlhttp.open("GET","calculate.php?type="+str+"&options1="options1+"&subs="+subs,true);
  xmlhttp.send();
};
<form class="form-horizontal" method="POST">
  <div class="box-body">
    <div class="form-group">
      <label for="inputEmail3" class="col-sm-2 control-label">Type</label>
      <div class="col-sm-10">
        <select class="form-control select2" name="type" id="itemtype" onchange="showoptionss(this.value)" style="width: 100%;">
          <option> Veg </option>
          <option> Non_veg </option>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="inputEmail3" class="col-sm-2 control-label">Class</label>
      <div class="col-sm-10">
        <select id="options1" class="form-control select2" name="class" style="width: 100%;">
        <?php
          $optionquery = "SELECT * FROM `prices` WHERE `dot`='Thaali' && `type`='Veg'";
          $optionquery_result = mysqli_query($con,$optionquery) or exit("Error in getting data");
          while($r = mysqli_fetch_array($optionquery_result)) {
            echo "<option>".$r['class']."</option>";
          }
        ?>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="inputPassword3" class="col-sm-2 control-label">Quantity</label>
      <div class="col-sm-10">
        <input type="number" class="form-control" id="quantity" required name="quantity" max="100" min="1" id="inputPassword3" placeholder="Quantity">
      </div>
    </div>
    <div class="form-group">
      <label for="inputPassword3" class="col-sm-2 control-label">Your Address</label>
      <div class="col-sm-10">
        <input type="text" class="form-control" required name="your_address" id="inputPassword3" placeholder="Your Address">
      </div>
    </div>
    <div class="form-group">
      <label for="inputPassword3" class="col-sm-2 control-label">Subscription</label>
      <div class="col-sm-10">
        <select class="form-control select2" value="<?php echo $subs; ?>" id="subs" name="subs" style="width: 100%;">
          <option> Today </option>
          <option> Monthly </option>
          <option> Quaterly </option>
          <option> Half_yearly </option>
          <option> Yearly </option>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="inputPassword3" class="col-sm-2 control-label">Time</label>
      <div class="col-sm-10">
        <input type="time" class="form-control" required value="<?php echo $time; ?>" name="time" id="inputPassword3" placeholder="Pickup Address">
      </div>
    </div>
    <div class="form-group">
      <label for="inputPassword3" name="total_cost" id="total_cost" required class="col-sm-2 control-label">Total Cost</label>
      <div class="col-sm-10">
        <input type="text" disabled value="<?php echo $total_cost; ?>" class="form-control" id="inputPassword3" placeholder="Total Cost">
      </div>
    </div>
  </div>
  <!-- /.box-body -->
  <div class="box-footer" id="f">

    <input type="button" class="btn btn-info" onclick="gettotalcost()" name="calculate" value="Calculate" />
    <input type="submit" class="btn btn-info pull-right" name="order" value="Order" />
  </div>
  <!-- /.box-footer -->
</form>

2 个答案:

答案 0 :(得分:0)

只是一个语法错误;)在}

处m myFunction1()

https://jsfiddle.net/6z9sp003/

度过愉快的一天

答案 1 :(得分:0)

失踪};在myFunction1()

的末尾