doupi4649 2016-09-29 09:15
浏览 38
已采纳

下拉使用jquery和sql更新另一个下拉列表

How can I update another dropdown with jquery with sql. It means from database.

  $('#typeOfGlass').on('change', function(){
   console.log($('#typeOfGlass').val());
    $('#glassWidth').html('');
    if($('#typeOfGlass').val()==15){
        $('#glassWidth').append('<option value="19">19</option>');
        $('#glassWidth').append('<option value="20">20</option>');
        $('#glassWidth').append('<option value="21">21</option>');
    }else{
        $('#glassWidth').append('<option value="6">6</option>');
        $('#glassWidth').append('<option value="7">7</option>');
        $('#glassWidth').append('<option value="8">8</option>');
        $('#glassWidth').append('<option value="9">9</option>');
    }
});

I use this one but i dont have idea how to make it from database.

$sqlq="SELECT DISTINCT daerah FROM kampung WHERE negeri='$negeri'";
    $result_set=mysqli_query($conn, $sqlq);
    while($row=mysqli_fetch_array($result_set, MYSQLI_ASSOC))
    {
        echo "<option value=".$row['daerah'].">".$row['daerah']."</option>";
    }

Any idea?

  • 写回答

2条回答 默认 最新

  • duandoucou7200 2016-09-29 09:26
    关注

    The first thing you need to do is to change the query or add info to your table so the program has a way to find out whether or not to append a specific <option> to the <select>.

    Next, fetch all rows into an array. This should work, but I didn't test it:

    $rows = array();
    while ($rows[] = mysqli_fetch_array($result_set, MYSQLI_ASSOC));
    

    Now pass this data to JavaScript:

    echo "<script> var data = " . json_encode($rows) . "; </script>";
    

    Finally, when #typeOfGlass is changed, clear #glassWidth, then iterate over data and append only the relevant elements, depending on #typeOfGlass's value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程