简体   繁体   English

根据上一个下拉列表中的选项在下拉列表中设置选项值

[英]Set option values in a dropdown list based on the option from previous dropdown

So there are two drop-down lists in my code.所以我的代码中有两个下拉列表。 One is "Specialization" which has options such as General, Cardiologist, Pediatrician, etc, and the other one is "Doctor" which has the name of the doctors as options.一种是“专科”,有一般、心脏病专家、儿科医生等选项,另一种是“医生”,有医生的名字作为选项。 What I want is to show the names of the doctors in the "Doctor" drop-down based on the values selected in the "Specialization" drop-down.我想要的是根据“专业化”下拉列表中选择的值在“医生”下拉列表中显示医生的姓名。

This is how my JSON looks like:这是我的 JSON 的样子:

[{"username":"Ashok","spec":"General"},{"username":"arun","spec":"Cardiologist"},{"username":"Dinesh","spec":"General"},{"username":"Ganesh","spec":"Pediatrician"},{"username":"Kumar","spec":"Pediatrician"},{"username":"Amit","spec":"Cardiologist"},{"username":"Abbis","spec":"Neurologist"}]

Eg: If I select 'General' from the "Specialization" drop-down, then only the doctors with General (Ashok and Dinesh) will be shown as the drop-down options in the "doctor" drop-down.例如:如果我从“专业化”下拉菜单中选择“通用”,那么“医生”下拉列表中的下拉选项中将只显示具有通用名称的医生(Ashok 和 Dinesh)。

Here is the HTML and JS part:这是 HTML 和 JS 部分:

<div class="col-md-4">
  <label for="spec">Specialization:</label>
</div>
      <div class="col-md-8">
         <select name="spec" class="form-control" id="spec">
           <option value="" disabled selected>Select Specialization</option>
           <?php display_specs();?>
         </select>
         <script>
             let data = <?php echo json_encode($docarray); ?>
             document.getElementById('spec').onchange = function updateSpecs(e) {
             let values = data.filter(obj => obj.spec == this.value).map(o => o.username);   
             document.getElementById('doctor1').value = document.querySelector(`[value=${//query that matches the doctor name in json}]`).getAttribute('data-value');
              };
            </script>
        </div><br><br>

<div class="col-md-4"><label for="doctor">Doctors:</label></div>
     <div class="col-md-8">
        <select name="doctor" class="form-control" id="doctor1" required="required">
           <option value="" disabled selected>Select Doctor</option>
           <?php display_docs();?>
        </select>
         <script>
           document.getElementById('doctor').onchange = function updateFees(e) {
           document.getElementById('docFees').value = document.querySelector(`[value=${this.value}]`).getAttribute('data-value'); // fees will be added based on the doctor mentioned in the db
              };

Here is the PHP code for 'display_specs()' and 'display docs()':这是“display_specs()”和“display docs()”的PHP代码:

function display_docs()
{
 global $con;
 $query="select * from doctb";
 $result=mysqli_query($con,$query);
 while($row=mysqli_fetch_array($result))
 {
  $username=$row['username'];
  $price=$row['docFees'];
  echo '<option value="' .$username. '" data-value="'.$price.'">'.$username.'</option>';
 }
}

function display_specs() {
  global $con;
  $query="select distinct(spec) from doctb";
  $result=mysqli_query($con,$query);
  while($row=mysqli_fetch_array($result))
  {
    $spec=$row['spec'];
    $username=$row['username'];
    echo '<option value="' .$username. '" data-value="'.$spec.'">'.$spec.'</option>';
  }
}

Screenshot:截屏: 在此处输入图片说明

I did my best my couldn't able to do that.我尽力了,我做不到。 So any suggestions are welcome.所以欢迎提出任何建议。 Thanks in advance!提前致谢!

You can use additional attribute in docs list data-spec="'.$spec.'"您可以在文档列表data-spec="'.$spec.'"使用附加属性

function display_docs()
{
 global $con;
 $query = "select * from doctb";
 $result = mysqli_query($con,$query);
 while( $row = mysqli_fetch_array($result) )
 {
  $username = $row['username'];
  $price = $row['docFees'];
  $spec = $row['spec'];
  echo '<option value="' .$username. '" data-value="'.$price.'" data-spec="'.$spec.'">'.$username.'</option>';
 }
}

function display_specs() {
  global $con;
  $query = "select distinct(spec) from doctb";
  $result = mysqli_query($con,$query);
  while($row = mysqli_fetch_array($result))
  {
    $spec = $row['spec'];
    $username = $row['username'];
    echo '<option value = "' .$username. '" data-value = "'.$spec.'">'.$spec.'</option>';
  }
}

then use JS to filter second select然后使用JS过滤第二个选择

 document.getElementById('spec').onchange = function foo() { let spec = this.value; let docs = [...document.getElementById('doctor').options]; docs.forEach((el, ind, arr)=>{ arr[ind].setAttribute("style",""); if (el.getAttribute("data-spec") != spec ) { arr[ind].setAttribute("style","display: none"); } }); };
 <div class="col-md-4"> <label for="spec">Specialization:</label> </div> <div class="col-md-8"> <select name="spec" class="form-control" id="spec"> <option value="" disabled selected>Select Specialization</option> <option value="General">General</option> <option value="Cardiologist">Cardiologist</option> <option value="Pediatrician">Pediatrician</option> <option value="Neurologist">Neurologist</option> </select> </div><br> <div class="col-md-4"><label for="doctor">Doctors:</label></div> <div class="col-md-8"> <select name="doctor" class="form-control" id="doctor" required="required"> <option value="" disabled selected>Select Doctor</option> <option value="Ashok" data-spec="General">Ashok</option> <option value="arun" data-spec="Cardiologist">arun</option> <option value="Dinesh" data-spec="General">Dinesh</option> <option value="Ganesh" data-spec="Pediatrician">Ganesh</option> <option value="Kumar" data-spec="Pediatrician">Kumar</option> <option value="Amit" data-spec="Cardiologist">Amit</option> <option value="Abbis" data-spec="Neurologist">Abbis</option> </select> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM