douxiza9868 2019-03-04 12:17
浏览 227

从数据库中填充的下拉列表中获取选定值

I want to get the selected SystemID from my drop down menu which is populated by a database. I have named the drop down"data3"

Drop down from database is all working, it is displaying all the SystemIDs from my table.

Code below:

<select id ="data3" style="position:relative; top:-70px; left: 3.6%;" 
name="data3" class="btn btn-light btn-sm dropdown-toggle">
    <option value="SystemID">SystemID</option>
    <?php
        $sql = "SELECT DISTINCT SystemID FROM MyTable ORDER BY 
        SystemID";
    $result = sqlsrv_query($conn, $sql);
    while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
        echo "<option value=\"\">" . $row['SystemID'] . "</option>";
        }
    ?>
  </select>

How do I get the selected value from that drop down? I have tried doing

var data3=$("#data3").val();

But this returns nothing

  • 写回答

1条回答 默认 最新

  • douzai1074 2019-03-04 12:27
    关注

    Use the ternary operator to check if the value coming from the database match the one from the select and mark it as selected. Please note that you must have a list of all the values of the select/option statement to be able to compare.

    $row['SystemID'] == $value?'selected':''

    echo "<option value='".$row['SystemID'] ."' ". ($row['SystemID'] == $value?'selected':'') ."  >" . 
            $row['SystemID'] . 
         "</option>";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程