为什么$(this).parent()在此元素上不起作用?

时间:2020-07-23 16:37:57

标签: javascript html jquery css

我有一组重复的n个项目,都采用这种形式:

<div class="col-md-6 col-sm-6 col-xs-12"><input type="hidden" name="line[5]" value="3">
  <input name="name[5]" type="text" style="width: 95%" value="Three" class="form-control col-md-7 col-xs-12">
  <a href="#" data="Delete picklist item" class="picklistitemDelete" attr="3|Three">
    <i class="fa fa-trash fa-lg" style="margin-top: 10px; margin-left: 5px;"></i>
  </a>
</div>

我想允许用户删除该项目,因此已将事件绑定到图像上的单击。这工作正常,但是一旦从数据库中删除该项目,我想隐藏该行。这就是我的做法:

      $(document).on("click", ".picklistitemDelete", function(){

            var dataString = $(this).attr('attr');
            var dataArray = dataString.split("|");

        $.confirm({
            title: 'Are you sure?',
            content: 'Are you sure you want to delete this pick list item: ' + dataArray[1] + '?',
            buttons: {
                confirm: function () {

                        $.ajax({
                            type: "POST",
                            url: '/ajax/Delete_ajax',
                            data: {'id' : dataArray[0], 'type' : 'pickitem', 'count' : dataArray[2]},
                            dataType: 'json',
                    success: function() {
                       $(this).parent().style.display = "none";
                       $.alert('Attribute ' + dataArray[1] + ' successfully deleted.');
                            },
                            error: function(xhr, response, error) {
                        $.alert('There was a problem deleting ' + dataArray[1] + '.');
                        }
                        });

                },
                cancel: function () {
                    //$.alert('Canceled!');
                }
            }
        });

        });

一切正常,但隐藏行的行:

$(this).parent().style.display = "none";

返回:

Uncaught TypeError: Cannot set property 'display' of undefined

我期望$(this).parent()是div。不是这样吗,如果可以的话,如何隐藏div?

0 个答案:

没有答案