【问题标题】:CHtml Link returns null for parentCHtml 链接为父级返回 null
【发布时间】:2014-01-12 18:59:18
【问题描述】:

我使用 Chtml::Link 创建了一个 url,如下所示:

CHtml::link("Remove", '#', array('class' => 'delete')))

现在,当用户点击它时,我会发送这样一个 ajax 请求:

$('.delete').click(function(e) {
    e.preventDefault();
    $.ajax({
      url:'" . $this->createUrl('//shop/shoppingCart/delete') . "',
      type : 'GET',
      data: {id: $position},
      success: function(result) {
         console.log($(this).parent());
      },
     });
});

它总是记录“undefined”,但链接在 td 内 我想要做的是删除包含链接的行。也欢迎任何新方法。请帮忙!

【问题讨论】:

    标签: javascript php jquery yii yii-components


    【解决方案1】:

    ajax 调用的success 函数中的this 是click 函数中的this 之外的另一个this。所以,你可能找不到那个this 的父级。

    我猜你正在尝试找到元素的父元素来更新 html 对吗?你为什么不使用 Chtml::ajaxLink 代替,那么你可以使用更新属性:

    Chtml::ajaxLink(
        'Remove',
        $this->createUrl('//shop/shoppingCart/delete'),
        array(
            'update' => "$('.delete').parent()", // or anything like this
            'data' => array(
                'id' => $position,
            )
        ),
        array(
            'class' => 'delete'
        )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-09
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多