doufen3563 2013-12-24 14:21
浏览 35
已采纳

CHtml Link为父级返回null

I created a url using Chtml::Link like this:

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

Now, when user clicks it i send a ajax request like this:

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

it always logs "undefined" but the link is inside a td What i want to do is remove the row containing the link. Any new approach is also welcome. Please help!

  • 写回答

1条回答 默认 最新

  • doutang7707 2013-12-24 14:28
    关注

    this inside the success function of the ajax call is another this than the this in the click function. So, you probably can't find the parent of that this.

    I guess you are trying to find the parent of the element to update the html right? Why don't you use Chtml::ajaxLink instead, then you can use the update property:

    Chtml::ajaxLink(
        'Remove',
        $this->createUrl('//shop/shoppingCart/delete'),
        array(
            'update' => "$('.delete').parent()", // or anything like this
            'data' => array(
                'id' => $position,
            )
        ),
        array(
            'class' => 'delete'
        )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?