2014-10-28 31 views
0

我想從postlink獲取id值。這裏是index.ctp刪除postlinkCakePHP 2.4:如何從cakephp中的postlink獲取id值?

<?php 

     echo $this->Form->postLink(

      $this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-remove')), 
      array('action' => 'delete', $user['User']['id'],'class'=>'del'), 
      array('escape'=>false,'class'=>'btn btn-sm btn-danger'), 
     __('Are you sure you want to delete # %s?', $user['User']['id']) 
    ); 

    ?> 

我已經jQuery的

$('document').ready(function(){ 
     var x=jQuery(this).attr("id"); 
     alert(x); 
}); 

在這裏,我沒有得到任何value.How得到這個ID爲AJAX提交試過婁代碼?

回答

0

您可以使用data-attribute

echo $this->Form->postLink(

     $this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-remove')), 
     array('action' => 'delete', $user['User']['id'],'class'=>'del'), 
     array('escape'=>false,'class'=>'btn btn-sm btn-danger', 'data-idVal' => $user['User']['id']), 
    __('Are you sure you want to delete # %s?', $user['User']['id']) 
); 

然後

$('document').ready(function(){ 
    var x=jQuery(this).data("idVal"); 
    alert(x); 
}); 
0

其實$這個 - >形式 - > postLink()沒有一個ID分配給刪除鏈接。你可以這樣做如下

echo $this->Form->postLink(

    $this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-remove')), 
    array('action' => 'delete', $user['User']['id'],'class'=>'del'), 
    array('escape'=>false,'class'=>'btn btn-sm btn-danger', 'id' => $user['User']['id']), 
    __('Are you sure you want to delete # %s?', $user['User']['id']) 
); 

而且讓你寫你的代碼相似的jQuery的。點擊事件ID到以下

$(".del").click(function() { 
    var x=jQuery(this).attr("id"); 
    alert(x); 
}); 
0

這不是一個很好的做法,但我有解決了這個問題的簡單方法

我有這個postlink改變

<button class="del" id=<?php echo $user['User']['id']; ?>>Delete </delete> 

現在我已經去tten此ID由

$('.del').click(function(){ 
var x=$(this).attr("id"); 
alert(X); 
}