dtdh11647 2015-03-06 18:27
浏览 40
已采纳

Yii独立linkBut​​ton不在表单标签内工作

I am using Yii and I have a form wrapped in tabs. In one of these tabs I need to put a link (call a controller/action with parameters) independent from the form content. I've tried to use CHtml::linkButton but it doesn't work.

The code scheme of the form is the following:

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'project-form',
    'enableAjaxValidation'=>true,
)); ?>

//...input elements

<?php echo CHtml::linkButton('Download',
    array(
        'submit'=>$this->createUrl('controller/action'),
        'params'=>array(
                'results'=>CJSON::encode(array('foo'=>'bar'))
        ))
); ?>

//...other input elements

<?php $this->endWidget(); ?>

When I click on the link nothing happens. If I put the linkButton code outside the form it works properly.

Is there a workaround for this?

  • 写回答

2条回答 默认 最新

  • dongxu1668 2015-03-08 00:20
    关注

    It looks like you're trying to use a button to let the user download some sort of file. For this situation AJAX won't work as it's not capable of saving files to a users computer.

    What you want to do is to create just the anchor tag. The function linkButton is designed for is to be a button that will submit the current form. If you're using bootstrap the below will also make it look like a button.

    CHtml::link('Download', array('controller/action', 'myparam' => 'paramvalue'), array('class', 'btn btn-primary'));
    

    Make sure you set the correct headers for downloading the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?