调用控制器并通过onlick侦听器传递值

时间:2018-05-24 02:44:16

标签: javascript php codeigniter

<?php 
    foreach($fetch_file as $row)
    {   
        echo '<tr>';
        echo '<td>' . base64_decode($row->file_perm_desc) . '</td>';
        echo '<td style = "text-align:center;">' . date_format((date_create($row->date_entry)),"M d, Y")  . '</td>';
        echo '<td class = "text-center"><a class="btn btn-info" >
        <input type = "hidden" name = "editid" class = "openid" value = ' . $row->file_perm_id . '>
        <i class="glyphicon glyphicon-folder-open"></td>';
        echo '<td class = "text-center"><a class="btn btn-warning" >
        <input type = "hidden" name = "editid" class = "unpublishid" value = ' . $row->file_perm_id . '>
        <i class="glyphicon glyphicon-comment"></td>';
        echo '<td class = "text-center"><a class="btn btn-danger" >
        <input type = "hidden" name = "editid" class = "deleteid" value = ' . $row->file_perm_id . '>
        <i class="glyphicon glyphicon-trash"></td>';
        echo '<td class = "text-center"><a class="btn btn-success" >
        <input type = "hidden" name = "editid" class = "downloadid" value = ' . $row->file_perm_id . '>
        <i class="glyphicon glyphicon-download"></td>';
        echo '</tr>';

     }      
?>  


  $('.btn-info').click(function()
    {
        var id = $(this).find('.openid').val();
        window.location.replace("<?php echo base_url();?>ClientCont/List_Files");


    });

我有来自上面的值的onlick监听器,用户点击它应该转到控制器的按钮,但我不知道如何调用控制器并从id传递值。这是在codeigniter框架中希望有人可以帮助谢谢

2 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助

  1. HTML代码错误。关闭锚标记(&#34; </a>&#34;)缺失。修复html如下

       '<td class = "text-center"><a class="btn btn-info" ><input type = "hidden" name = "editid" class = "openid" value = ' . $row->file_perm_id . '><i class="glyphicon glyphicon-folder-open">**</a>**</td>'
    
  2. 尝试以下javascript代码。

    $(&#39; .btn-info&#39;)。点击(function(){

      var id = $(this).find('input').val();
      var url = "<?php echo base_url();>ClientCont/List_Files?id="+id;
      window.location.replace(url);
    

    });

答案 1 :(得分:0)

希望这会对您有所帮助:

$('.btn-info').click(function() {
  var id = $(this).find('.openid').val();
  if (id) 
  {
     window.location.href = "<?php echo base_url('ClientCont/List_Files/');?>" + id;
  }

});

您的控制器的List_Files方法应如下所示:

public function List_Files($id)
{  
   /*echo passed id from the js here like this */

   echo $id;
}