动态填充<select>,同时不显示所有<option> </option> </select>

时间:2013-09-09 08:44:22

标签: php jquery-mobile select option

我有一个动态填充id = antibiotic的选择锚。根据我想要的选择,然后用id = dose动态填充下一个listview。下面的代码是有效的,但是为id = dose动态选择的所有选项锚点同时显示。我已经尝试以编程方式选择一个选项然后刷新无济于事。还有其他想法吗?我正在使用jquery mobile。

<body>
    <select Onchange='changer($(this).val())' class="antibiotic" id="antibiotic"  data-native-menu="false">
        <option value="">Select Antibiotic</option>          
        <?php
            $hostname = "xxxx";
            $username = "xxxx";
            $dbname = "xxxx";
            $password = "xxxx";
            $usertable = "renalAdjust";
            $yourfield = "antimicrobial";   
            //Connecting to your database
            $link = mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later.");
            mysql_select_db($dbname);
            //Fetching from your database table.
            $query = "SELECT * FROM $usertable";
            $result = mysql_query($query);
            if ($result) {
                $antimicrobial = array();
                while($row = mysql_fetch_array($result)) {
                    $name = $row["$yourfield"];
                    array_push($antimicrobial, $name);
                }
                mysql_close($link);
                $antimicrobial2 = array_unique($antimicrobial);                    
                foreach($antimicrobial2 as $item) {
                    $item2 = str_replace(" ","_", $item);
                    ?><option class='opt' value=<?php echo $item2 ?>><?php echo $item ?></option><?php
                }
            }?>   
            </select>     
            <select class="dose" id="dose"  data-native-menu="false">
                <option value="">Select Dose</option>
            </select>   
            <select id="Creatinine Clearance"  data-native-menu="false">
                <option value="">Select Creatinine Clearance</option>
            </select> 
    <script>
        function changer(val){
            var dataString = 'id='+ val.replace("_"," ","g");
            $.post("renalAdjust2.php", {id: dataString}, function(data){
                var doses = data.split("----");
                for (var i in doses){
                    $('<option>').val(doses[i]).text(doses[i]).appendTo('.dose');
                }
            });
        }
    </script>   
</body>

1 个答案:

答案 0 :(得分:0)

data-native-menu =“false”有效。在移动浏览器上它仍然可以很好地呈现......