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

如何从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条)

相关推荐 更多相似问题