Yii CHtml :: link()示例

时间:2014-12-02 14:31:02

标签: php yii

我有表格,2 CHtml::link()有不同的网址。

我的表单方法是get。

我想要的是:点击1 CHtml::link()时 - 使用方法get将表单提交到example.com/first 我想要的是:点击2 CHtml::link()时 - 使用方法发布

将表单提交到example.com/second

这可能吗?我的意思是我需要针对不同的提交按钮和操作的更改表单方法。

1 个答案:

答案 0 :(得分:1)

您可以通过javascript代码提交表单:

$('#myLink1', '#myLink2').on('click', function(e){
    e.preventDefault();
    var method = $(this).attr('href')=='example.com/first' ? 'GET':'POST';
    $('#myFrom').attr(
        'action', 
        $(this).attr('href') //href attribute should contain appropriate url
    ).attr(
        'method', 
        method
    ).submit();
});

此外,您可以使用jquery form plugin以ajax方式发送表单:

$('#myLink1').on('click', function(e){
    e.preventDefault();
    $('#myForm').ajaxSubmit({
         url: $(this).attr('href'),
         type: 'GET',
         success: function(){/*your code here*/}
    });
});
$('#myLink2').on('click', function(e){
    e.preventDefault();
    $('#myForm').ajaxSubmit({
         url: $(this).attr('href'),
         type: 'POST',
         success: function(){/*your code here*/}
    });
});