从下拉菜单中获取值

时间:2020-04-24 01:57:34

标签: javascript html jquery

我想从下拉菜单(#city)中获取一个值并将其放在另一个下拉菜单(#type)的onchange属性中

我已经尝试过对同一问题进行一些回答,但是当我传入mine的字符串值时,它们无法正常工作,而代码也可以正常工作

<div>
  <label for="city">City<label>
  <select class="form-control" id="city" name="city" onchange="getTypo(this.value)">
    <option value="">--Choose a city--</option>
    <?php
    $city = "SELECT DISTINCT `city` FROM `terrains` ORDER BY `city` ";
    $out = mysqli_query($db,$city);
    if (mysqli_num_rows($out) > 0) {
      while ($row = mysqli_fetch_assoc($out)) {
        ?><option value= <?php echo $row['city']?>><?php echo $row['city']?></option><?php
      }
    }
    ?>
  </select>
</div>
<div class="type">
  <label for="type">Type<label>

  <select class="form-control" id="type" name="type" onchange="getSTD(this.value ,'~~~??')">
    <option value="">--Choose a type--</option>

  </select>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

我不知道这是否是正确的方法,但这是我解决的方法: 我用一个功能来存储城市

~~~~html
                <div>
                  <label for="city">City<label>
                  <select class="form-control" id="city" name="city" onchange="getTypo(this.value);storeCity();">
                    <option value="">--Choose a city--</option>
                    <?php
                    $city = "SELECT DISTINCT `city` FROM `terrains` ORDER BY `city` ";
                    $out = mysqli_query($db,$city);
                    if (mysqli_num_rows($out) > 0) {
                      while ($row = mysqli_fetch_assoc($out)) {
                        ?><option value= <?php echo $row['city']?>><?php echo $row['city']?></option><?php
                      }
                    }
                    ?>
                  </select>
                </div>
                <div class="type">
                  <label for="type">Type<label>
                  <select class="form-control" id="type" name="type" onchange="getSTD(this.value , storeCity())">
                    <option value="">--Choose a type--</option>
                  </select>
                </div>

这是storeCity函数:

function storeCity(){
  var x = document.getElementById("city");
  var val = x.options[x.selectedIndex].value;
  return val;
}