2017-08-31 48 views
1

我在点击两次后禁用下载链接时遇到了问题。禁用功能是否工作正常,但有一个问题,因为下面提到如何禁用2次点击后下载链接?

  • 如果禁用正在2次点击,文件下载将停止其使用PHP
  • 如果下载的作品,关闭工作不

它对我来说是无论或者是情况。我在下面放置我的代码。

var preventClick = false; 
 
var howMany = 1; 
 
$('.ThisLink').click(function(e) { 
 
    howMany += 1; 
 
    if (howMany == 3) { 
 
    $(this) 
 
     .css('cursor', 'default') 
 
     .css('text-decoration', 'none') 
 
    } 
 
    /*if (!preventClick) { 
 
     $(this).html($(this).html() + ' lalala'); 
 
    } 
 

 
    preventClick = true;*/ 
 

 
    return false; 
 
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> 
 
<table id="datatable" class="table table-striped table-bordered"> 
 
    <thead> 
 
     <tr> 
 
     <th>#</th> 
 
     <th>Report</th> 
 
     <th>Action</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <?php 
 
     $x = 1; 
 
      
 
      foreach ($h->result() as $row) 
 
      
 
      { $ids = explode(',',$row->report); 
 
      
 
      $temp = sizeof($ids); 
 
      
 
      for($i =0; $i < $temp ; $i++) 
 
      { ?> 
 
     <tr> 
 
     <td> 
 
      <?php echo $x++;?> 
 
     </td> 
 
     <td> 
 
      <?php echo $ids[$i];?> 
 
     </td> 
 
     <td> 
 
      <!-- <?php echo base_url()?>uploadFiles/<?php echo $ids[$i]; ?> --> 
 
      <a href="<?php echo base_url()?>uploadFiles/<?php echo $ids[$i]; ?>?>" target="_blank" class="btn btn-info ThisLink" download>Download</a> 
 
      <!-- <button id="my_button">Click Here</button> --> 
 
     </td> 
 
     <?php } ?> 
 
     </tr> 
 
     <?php } 
 
     ?> 
 
    </tbody> 
 
</table>

+0

请格式化你的Q无需要PHP代码也是不需要的代码片段ID不起作用。对于标签,你必须将属性href设置为值href =“#”或href =“javascript:void()”... –

回答

2

你觉得这个怎么样:

CSS:

<style type="text/css"> 
    .disabled{ 
     background-color: gray; 
     pointer-events: none; 
    } 
</style> 

HTML:

<a href="downloadLocation.ext" id="autoIncrementWithPhp" class="yourClassLink">Link</a> 

JS:

<script type="text/javascript"> 
    //Create empty array 
    var arrayRememberClick=[]; 

    //Add all link id to this array and initialise counter to 0 
    $('.yourClassLink').each(function(){ 
     var idCurrent = $(this).attr('id'); 
     arrayRememberClick[idCurrent]=0; 
    }); 

    //Detect click 
    $('.yourClassLink').click(function(event){ 
     var idClicked = $(this).attr('id'); 
     arrayRememberClick[idClicked] += 1; 
     if(arrayRememberClick[idClicked]>=2){ 
      $('#'+idClicked).addClass('disabled'); 
     } 
    }); 

+0

嘿谢谢你回复,但你有没有测试过? –

+0

当然,看看我的小提琴:https://jsfiddle.net/b6u1fcu9/ – Azee

+0

它的工作非常感谢 –