douzhao2047 2018-08-25 06:39
浏览 287
已采纳

如何从JavaScript循环中获取Select选项的值

I want to get / fetch value of select option from for-each loop in JavaScript

code

 <?php $user = $this->admin_model->getData('user'); ?>

 <select class="form-control" name="user" id="user">
        <option value="">Select User to Assign</option>
           <?php foreach ($user as $row): ?>
               <option value="<?php echo $row->id; ?>"><?php echo $row->user_name</option>
           <?php endforeach; ?>
 </select>
  • 写回答

2条回答 默认 最新

  • dongzhi6382 2018-08-25 07:47
    关注

    Try this.

    <?php $user = $this->admin_model->getData('user'); ?>
    
     <select class="form-control" name="user" id="user" onchange="getValue(this)">
        <option value="">Select User to Assign</option>
           <?php foreach ($user as $row): ?>
               <option value="<?php echo $row->id; ?>"><?php echo $row->user_name;?></option>
           <?php endforeach; ?>
    </select>
    
    <script>
    function getValue(sel){
       var optionvalue = sel.value;
    
        alert(optionvalue);
    
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 滑块验证码拖动问题悬赏
  • ¥15 Wanted but not invoked:Actually, there were zero interactions with this moc
  • ¥20 怎么驱动tb6612
  • ¥15 Arcgis 3D效果点要素
  • ¥15 在执行yolo训练脚本的时候,没有报错,在扫描val这一步后就推出执行了
  • ¥15 delphi开发的WEBSERVER改用HTTPS协议
  • ¥15 pic16f877A单片机的计数器proteus仿真失效
  • ¥100 调查 Vitis AI 中验证 .xmodel 量化后的正确性
  • ¥30 codelite全屏时file、setting那一行消失了
  • ¥15 gazebo-rviz教程