【问题标题】:Yii CHtml::link() exampleYii CHtml::link() 例子
【发布时间】:2014-12-02 14:31:02
【问题描述】:

我有表单和 2 个CHtml::link() 具有不同的网址。

我的表单方法是get。

我想要什么:当点击 1 CHtml::link() - 提交表单到 example.com/first 使用方法 get 我想要什么:点击 2 CHtml::link() 时 - 使用 post 方法将表单提交到 example.com/second

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

【问题讨论】:

    标签: php yii


    【解决方案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*/}
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多