JavaScript 检索表字段到模态窗口进行编辑?

标签 javascript jquery

所以基本上我想要实现的是一个将具有编辑和删除按钮的表,该按钮将显示一个模式窗口,要么是用于编辑的表单,要么是用于删除的对话框。我可以自己解决删除问题,但编辑问题更难。

这是我当前的表:

<table id="customersTable" class="table table-striped">
  <thead>
      <tr>
          <th>UserID</th>
          <th>Position</th>
          <th>Lol Region</th>
          <th>Name</th>
          <th>Email</th>
          <th>Edit</th>
          <th>Delete</th>
      </tr>
  </thead>
  <tbody>
    <?php
      foreach ($customers as $customer): ?>
        <tr id="<?php echo $customer->user_id; ?>">
          <td><?php echo $customer->user_id; ?></td>
          <td><?php echo $customer->type; ?></td>
          <td><?php echo $customer->region; ?></td>
          <td><?php echo $customer->full_name; ?></td>
          <td><?php echo $customer->email; ?></td>
          <td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-pencil"></span></button></p></td>
          <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p></td>
        </tr>
      <?php endforeach;
    ?>
  </tbody>
</table>

还有我的模态形式:

<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
  <h4 class="modal-title custom_align" id="Heading">Edit Customers Details</h4>
</div>
<div class="modal-body">
  <div class="form-group">
    <input class="form-control " type="text" placeholder="">
  </div>
  <div class="form-group">
    <input class="form-control " type="text" placeholder="">
  </div>
  <div class="form-group">
    <textarea rows="2" class="form-control" placeholder=""></textarea>
  </div>
</div>
  <div class="modal-footer ">
    <button type="button" class="btn btn-warning btn-lg" style="width: 100%;"><span class="glyphicon glyphicon-ok-sign"></span> Update</button>
  </div>
</div>
<!-- /.modal-content -->
</div>
  <!-- /.modal-dialog -->
</div>

因为我不太擅长 Javascript,所以我假设我可能必须在包含单击按钮的 tr 中选择最接近的值,但我不确定。

建议如何解决这个问题? -- 请注意,表单将 POST 到一个 PHP 文件,因此我将需要以某种方式重绘该表,因为它是一个数据表。

谢谢

最佳答案

要通过 class 或 id 访问数据,您还有很多工作要做。我会像这样修改您的 html

 <?php
      foreach ($customers as $customer): ?>
        <tr id="<?php echo $customer->user_id; ?>">
          <td class='userid'><?php echo $customer->user_id; ?></td>
          <td class='usertype'><?php echo $customer->type; ?></td>
          <td class='region'><?php echo $customer->region; ?></td>
          <td class='fullname'><?php echo $customer->full_name; ?></td>
          <td class='email'><?php echo $customer->email; ?></td>
          <td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-primary btn-xs edit" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-pencil"></span></button></p></td>
          <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs delete" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p></td>
        </tr>
      <?php endforeach;
    ?>

也给你编辑和删除一个类

然后使用 jQuery

$(document).ready(function(){
   $('.edit').click(function(){
     alert($(this).closest('tr').find('.userid').html()); //you will get the userid
     //same way you can get the remaining values ,just change the find parameter with its classname
   //now assign it to the form value
  $('#your_model_form_value_username').val($(this).closest('tr').find('.username').html());
//same way do the rest
   });
});

关于JavaScript 检索表字段到模态窗口进行编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33622607/

相关文章:

javascript - 如何将 Highchart 放到 html 页面

javascript - 从Javascript中的Kendo网格中的列名获取列索引

javascript - 我如何从ajax加载的JQuery ui选项卡中获取此方法

javascript - 传递包含变量的 HTML 元素以替换函数

jquery - 如何使用即席对话框包含并提交表单

javascript - 如何通过关系获取 prisma 中字段的总和

javascript - D3弧形中心的弯曲标签

jQuery 将鼠标悬停在子元素上会产生奇怪的效果

javascript - 构建时间 slider ,不知道从哪里开始

javascript - 对象、数组和 $.each()