duanqiang3925 2015-04-26 09:07
浏览 105

如何将JavaScript结果转换为显示下拉列表为<option val =“value”> Text </ option>

Below is the code for a dropdown list. The second dropdown results populate based on the first dropdown selection. Everything is working fine using this code.

But the issue is that it shows hospital_id on the second dropdown as text and also as value. I want to keep the value as hospital_id, but how to change the text to hospital_name (this is another column in table mfb_hospital).

My Code:

<?php
  $db = new mysqli('localhost','root','redhat','echodeve_mfb_temp');//set your database handler
  $query = "SELECT bp_id,bp_name FROM mfb_billing";
  $result = $db->query($query);

  while($row = $result->fetch_assoc()){
    $categories[] = array("bp_id" => $row['bp_id'], "val" => $row['bp_name']);
  }

  $query = "SELECT bp_id, hospital_id, hospital_name FROM mfb_hospital";
  $result = $db->query($query);

  while($row = $result->fetch_assoc()){
    $subcats[$row['bp_id']][] = array("bp_id" => $row['bp_id'], "val" => $row['hospital_id']);
  }

  $jsonCats = json_encode($categories);
  $jsonSubCats = json_encode($subcats);


?>

<!docytpe html>
<html>

  <head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
    <script type='text/javascript'>
      <?php
        echo "var categories = $jsonCats; 
";
        echo "var subcats = $jsonSubCats; 
";
      ?>
function loadCategories(){
        var select = document.getElementById("categoriesSelect");
        select.onchange = updateSubCats;
        for(var i = 1; i < categories.length; i++){
          select.options[i] = new Option(categories[i].val,categories[i].bp_id);          
        }
      }
      function updateSubCats(){
        var catSelect = this;
        var catid = this.value;
        var subcatSelect = document.getElementById("subcatsSelect");
        subcatSelect.options.length = 0; //delete all options if any present
        for(var i = 0; i < subcats[catid].length; i++){
          subcatSelect.options[i] = new Option(subcats[catid][i].val,subcats[catid][i].hosp);
        }
      }
    </script>
<script>
  $(function() {
    $( "#from" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
        dateFormat: "yy-mm-dd",
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
        dateFormat: "yy-mm-dd",
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });
  </script>
</head>

  <body onload='loadCategories()'>
   <form id="reportvalue" action="backend.php" method="post">

    <select id='categoriesSelect'>
        <option value="1">Select Billing Provider</option>
    </select>

    <select name='hospitalname[]' id='subcatsSelect' multiple='multiple'>
        <option value="all">Select Billing Provider</option>
    </select>
<label for="from">From</label>
<input type="text" id="from" name="from">
<label for="to">to</label>
<input type="text" id="to" name="to">
<?php
//$a = $_REQUEST['hospitalname[]'];
//echo $a;
?>
   <input type="submit" name="Submit" value="Submit">
  </form>
  </body>
</html>
  • 写回答

1条回答 默认 最新

  • douyakao5308 2015-04-26 09:44
    关注

    I'm suggesting id you use .append function a simple example is coded below:

            $.each(json, function(){
    
            $('#subcatsSelect').append("<option value='"+json.id+"'>"+json.name+"</option>");
        });
    

    note that :

    $.each(json, function() is just a sample for your action to populate the second select options.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化