在模态中打开更新时将selectbox设置为已选中

时间:2016-12-05 07:48:42

标签: javascript jquery codeigniter-3

我在这里通过模态及其工作更新数据,但事实是,我想将所选数据显示为已选中。更新功能正在运行,但没有想到要选择的值。

以下是我的观点

<td><a class="btn btn-primary vehicle_user" data-toggle="modal" data-target="#myModaledit" id="<?php echo $row->id;?>"  vehicle="<?php echo $row->vehicle_id; ?>">Edit</a></td>

这是我的模态

<div class="modal fade" id="myModaledit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Edit Vehicle</h4>
      </div>
      <form action="<?php echo base_url();?>vehicle/vehicle_control/edit_vehicle_user" method="post">
        <div class="modal-body">


          <div class="form-group">
            <label for="vehicle" class="control-label">Vehicle:</label>
            <select name="vehicle" class="form-control">
              <?php foreach($vehicles as $res){?>
                <option  <?php if($res->id==$row->vehicle_id)echo 'selected' ?>  value="<?php echo $res->id;?>"><?php echo $res->name;?></option>
              <?php }?>
            </select>
          </div>
      </div>
        <div class="modal-footer">
          <input type="hidden" name="id" value="" id="vehicle_user_id">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="submit" class="btn btn-primary">Save changes</button>
        </div>
      </form>
    </div>
  </div>
</div>

这是我的jQuery脚本

$(document).ready(function() {
    $(".vehicle_user").click(function() {
        var id = $(this).attr('id');
        //alert(id);

        $("#vehicle_user_id").val(id);
        $("#vehicle").val($(this).attr('vehicle'));
    });
});

0 个答案:

没有答案