无法使JavaScript函数与下拉列表一起使用

时间:2019-04-19 21:10:25

标签: javascript html html5

我在使我的JavaScript能够正确使用html时遇到问题。我需要“活动”下拉框与“城市”下拉框配合使用。例如,如果我选择“布里斯班”,然后单击活动框,则选项应为“城市游览,体育,骑自行车,博物馆,划船”,并且根据选择的城市而有所不同。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Reservations</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="travel.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  <link rel="icon" href="travel.png">
  <style type="text/css">
    body{
      background-color: white;
    }

  </style>
</head>
<header>

  <img class="imageheader" src="travel.PNG" alt="megatravel" align="left" width="145" height="145">
  <div class="wrapper">
    <nav role="navigation">
      <h1><a href="travelindex.html">Mega Travel</a></h1>
      <ul class="nav-ul">
        <li><a href="travelindex.html">Home</a></li>
        <li><a href="aboutus.html">About Us</a></li>
        <li><a href="reservations.html">Reservations</a></li>
      </ul>
      <a href="aboutus.html"><a/>
       <a href="travelindex.html"><a/>
       </nav>
     </div>
   </header>

   <body>
    <div class="container">
     <div class="col-4">
      <h1><strong>Book a Reservation</strong></h1>
      <form action="contactform.php" method="post" id="reservationform">

        <div class="form-group">
          <label for="name">Name</label>
          <input type="text" id="name" class="form-control">
        </div>

        <div class="form-group">
          <label for="email">Email</label>
          <input type="text" id="email" class="form-control">
        </div>

        <div class="form-group">
          <label for="phone">Phone Number</label>
          <input type="text" name="phoneNum" id="phone" class="form-control">
        </div>

        <div class="form-group">
          <label for="adults">Adults</label>
          <input type="number" name="adultsNo" id="adults" class="form-control">
        </div>

        <div class="form-group">
          <label for="children">Children</label>
          <input type="number" name="children" id="children" class="form-control">
        </div>

        <div class="form-group">
          <label for="city">City</label>
          <select name="city" id="city"  style="width: 250px" onchange="activitiesSelect()" class="form-control">
            <option selected disabled>Select</option>
            <option value="Brisbane">Brisbane</option>
            <option value="Vancouver">Vancouver</option>
            <option value="New_York_City">New York City</option>
            <option value="Berlin">Berlin</option>
            <option value="Cancun">Cancun</option>
          </select>
        </div>

        <div class="form-group">
          <label for="activity">Activity</label>
          <select name="activity" id="activity" style="width: 250px" class="form-control">
            <option selected disabled>Select</option>
          </select>
        </div>

        <div class="form-group">
          <label for="traveldate">Travel Date</label>
          <input type="date" name="traveldate" id="traveldate" style="width: 250px" class="form-control">
        </div>

        <div class="form-group">
          <button type="submit" form="reservationform" value="Submit" style="width: 100px">Submit</button>
        </div>
      </form>

    </div>
  </div>
  <script src="megatravel.js"></script>
</body>
</html>

function myfunct() {
  var docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
  var cur_height=window.innerHeight;
  var total_height=docHeight-cur_height;
  alert(total_height/100);
//var amout=total_height/100;
}
{
  var Brisbane = ["City Tours","Sports","Cycling","Museums","Boating"];
  var Vancouver = ["Sailing","Beach","Hiking","Museums","Boating"];
  var New_York_City = ["Museums", "Theatre", "Parks and Recreation", "City Tours"];
  var Berlin = ["City Tours", "Museums", "Cycling"];
  var Cancun = ["Parks and Recreation", "Beaches", "Boating", "Snorkeling"];
}
function activitiesSelect(){
  var city=document.getElementById('city').value;
  $('#activity').find('option').remove().end().append('<option value="none" disabled selected>Select</option>').val('none');

  if(city=="Brisbane"){
    for(i=0;i<Brisbane.length;i++){
      var o = new Option(Brisbane[i], Brisbane[i]);
      $(o).html(Brisbane[i]);
      $("#activity").append(o);
    }
  }
  else if(city=="Vancouver"){
    for(i=0;i<Vancouver.length;i++){
      var o = new Option(Vancouver[i], Vancouver[i]);
      $(o).html(Vancouver[i]);
      $("#activity").append(o);
    }
  }else if(city=="New_York_City"){
    for(i=0;i<New_York_City.length;i++){
      var o = new Option(New_York_City[i], New_York_City[i]);
      $(o).html(New_York_City[i]);
      $("#activity").append(o);
    }
  }else if(city=="Berlin"){
    for(i=0;i<Berlin.length;i++){
      var o = new Option(Berlin[i], Berlin[i]);
      $(o).html(Berlin[i]);
      $("#activity").append(o);
    }
  }else if(city=="Cancun"){
    for(i=0;i<Cancun.length;i++){
      var o = new Option(Cancun[i], Cancun[i]);
      $(o).html(Cancun[i]);
      $("#activity").append(o);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

考虑像这样更改JavaScript:

const cities = {
  Brisbane: ["City Tours", "Sports", "Cycling", "Museums", "Boating"],
  Vancouver: ["Sailing", "Beach", "Hiking", "Museums", "Boating"],
  Berlin: ["City Tours", "Museums", "Cycling"],
  Cancun: ["Parks and Recreation", "Beaches", "Boating", "Snorkeling"]
};

function activitiesSelect(e) {
  const { value: city } = e.target;
  const values = cities[city];

  $("#activity")
    .find("option")
    .remove()
    .end()
    .append('<option value="none" disabled selected>Select</option>')
    .val("none");

  for (let i = 0; i < values.length; i++) {
    var o = new Option(values[i], values[i]);
    $(o).html(values[i]);
    $("#activity").append(o);
  }
}

document.getElementById("city").addEventListener("change", activitiesSelect);

然后从您的选择标记中删除onchange处理程序。

这使您的JavaScript和HTML分开,并且使用city对象避免了if ... else语句过多。

https://codesandbox.io/embed/m7q1kqq9j9?fontsize=14