将数据传递给模态,然后在调用控制器不工作时使用数据作为参数

时间:2018-03-14 12:44:41

标签: javascript php codeigniter

查看:

<head>

 <script>
  var ABSOLUTE_PATH = '<?php echo base_url()?>';
 </script>

 </head>

//表内数据

<button type="button" class="btn btn-danger btn-sm open-deleteModal" data-id="<?php echo $news['id']; ?>" data-toggle='modal' data-target="#deleteModal"><i class="fa fa-fw fa-trash-o"></i> Delete
 <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Delete Record</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    </div>
                    <div class="modal-body">                      
                    Are you sure you want to delete this record?</div>
                    <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" id="delRowId">Delete</a>
                    </div>
                </div>
                </div>
    </div>

javascript放在页脚中   

  $(document).on("click", ".open-deleteModal",function(){
      var rowId = $(this).data('id');
      $(".modal-body #delRowId").attr("href",ABSOLUTE_PATH+"admins/deletenews/"+rowId);
  });
</script>

控制器:admins.php

  public function deletenews($rowid){
                $this->load->view('templates/admin_header');
                $this->load->view('admin_pages/view');
                $this->load->view('templates/admin_footer');
        }

<a class="btn btn-primary" id="delRowId">Delete</a>应该是调用javascript的人,但它不会调用控制器。请帮帮我......

1 个答案:

答案 0 :(得分:0)

这只是将href更改为正确的链接

$(".modal-body #delRowId").attr("href",ABSOLUTE_PATH+"admins/deletenews/"+rowId);

更改propoer链接的href后需要触发click事件。试试这样的事情

 $(".modal-body #delRowId").attr("href",ABSOLUTE_PATH+"admins/deletenews/"+rowId).click();