2013-08-01 30 views
1

我是yii的新手,遇到了一個問題,這讓我很沮喪。我一直在尋找,但沒有運氣。我有低於這個GridView控件:yii gridview中的JavaScript事件全部被解僱一次

$this->widget('bootstrap.widgets.TbGridView',array(
    'id'=>'leaves-grid', 
    'template' => "<div>{pager}</div><div style='float:left;'>{summary}</div><div class='clear'>&nbsp;</div>\n{items}\n<div>{pager}</div><div style='float:left;'>{summary}</div><div class='clear'>&nbsp;</div><br/>", 
    'dataProvider'=>$model->search(), 
    'columns'=>array(
     'id_user', 
     'id_leaves_type', 
     'leaves_from', 
     'leaves_to', 
     'leaves_desc', 
     'leaves_status', 
     array(
      'class'=>'bootstrap.widgets.TbButtonColumn', 
      'template' => '<div class="btn-group">{update}{approve}{disapprove}{cancel}</div>', 
      'buttons' => array(
      'update' => array(
       'label' => 'Edit', 
       'options' => array('class'=>'btn', 'rel' => ''), 
       'visible' => '$data->leaves_status == "Pending"' 
      ), 
      'approve' => array(
       'label' => 'Approve', 
       'icon' => 'ok', 
       'options' => array('class'=>'btn btn-delete', 'rel' => ''), 
       'click'=>'function(){return confirm("Are you sure you would like to approve this leave?");}', 
       'url' => 'CController::createUrl("//hrm/leaves/leaveaction", array("id"=>$data->id_leaves, "type"=>"Approved"))', 
       'visible' => '$data->leaves_status == "Pending"' 
      ), 
      'disapprove' => array(
       'label' => 'Disapprove', 
       'icon' => 'hand-down', 
       'options' => array('class'=>'btn btn-delete', 'rel' => ''), 
       'click'=>'function(){return confirm("Are you sure you would like to disapprove this leave?");}', 
       'url' => 'CController::createUrl("//hrm/leaves/leaveaction", array("id"=>$data->id_leaves, "type"=>"Not Approved"))', 
       'visible' => '$data->leaves_status == "Pending"' 
      ), 
      'cancel' => array(
       'label' => 'Cancel', 
       'icon' => 'remove', 
       'options' => array('class'=>'btn btn-delete', 'rel' => ''), 
       'click'=>'function(){return confirm("Are you sure you would like to cancel this leave?");}', 
       'url' => 'CController::createUrl("//hrm/leaves/leaveaction", array("id"=>$data->id_leaves, "type"=>"Cancelled"))', 
       'visible' => '$data->leaves_status == "Pending"' 
      ), 
      ), 
      'htmlOptions'=>array('style'=>'width: 150px; text-align: center;'), 
     ), 
    ), 
)); 

基本上有4個按鈕在GridView控件的最右邊一欄:編輯,批准,否決,並取消之前要列出的URL會提示確認框。

問題是,當我點擊其中一個按鈕時,所有點擊事件被觸發並且確認框全部出來。什麼時候出錯?請幫助...

回答

1

因爲您的代碼將呈現以下JS

$(document).on('click','#leaves-grid a.btn.btn-delete',function(){return confirm("Are you sure you would like to approve this leave?");}); 
$(document).on('click','#leaves-grid a.btn.btn-delete',function(){return confirm("Are you sure you would like to disapprove this leave?");}); 
$(document).on('click','#leaves-grid a.btn.btn-delete',function(){return confirm("Are you sure you would like to cancel this leave?");}); 

.....

,將利用最後一類分配給點擊事件,那麼你可以看到,所有這些生成選擇器都是一樣的。爲了避免這種情況,你可以在屬性類的末尾添加特定的類看起來像

'class'=>'btn btn-approve' 
'class'=>'btn btn-disapprove' 
'class'=>'btn btn-cancel' 
+0

啊,是的,我正在做一些複製粘貼按鈕,並沒有注意到。非常感謝你! –