1

我是 yii 的新手,遇到了一个让我很沮丧的问题。我一直在到处寻找,但没有运气。我在下面有这个gridview:

$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;'),
        ),
    ),
));

基本上,gridview 的最右列有 4 个按钮:Edit、Approve、Disapprove 和 Cancel,并且在转到列出的 URL 之前会提示确认框。

问题是,当我单击其中一个按钮时,所有单击事件都会被触发,并且确认框都会一个接一个地出现。到底什么时候错了?请帮忙...

4

1 回答 1

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'
于 2013-08-01T12:40:09.407 回答