2012-10-14 37 views
5

Tôi có liên kết đăng một số dữ liệu lên bộ điều khiển. Nó hoạt động tốt và cập nhật dữ liệu. Nhưng tôi muốn thay đổi css của div cha nhưng nó không xảy ra. Tôi đã thử rất nhiều biến thể nhưng tôi phải làm điều gì đó ngu ngốc.Bố mẹ jquery/gần nhất không hoạt động

Mã dưới

Xem:

@foreach (var item in Model) 
{ 
    <div class="added-property-excerpt"> 
     ... 
     <div class="added-property-links"> 
     ... 
     @if (!item.IsPropertyDisabled) { 
       @Html.ActionLink("Take off the Market" , "Disable", "Property", new { id = item.PropertyId }, new { id ="disable-link" }) 
      } 
      else { 
       @Html.ActionLink("Bring on the Market" , "Enable", "Property", new { id = item.PropertyId }, new { id ="enable-link" }) 
      } 
     </div> 
    </div> 
} 

JQuery

<script> 
    $(function() { 
     $('a#disable-link').click(function (e) { 
      $('.added-property-links').text('loading...'); 
      $.ajax({ 
       url: this.href, 
       dataType: "text json", 
       type: "POST", 
       data: {}, 
       success: function (data, textStatus) { } 
      }); 
      $(this).closest('.added-property-excerpt').css("background", "red"); 
      // $(this).parent('.added-property-excerpt').css("background", "red"); 
      e.preventDefault(); 
     }); 
    }); 
</script> 
+3

Bạn có thể đăng mẫu HTML đã xuất không? –

+1

Bạn đã ràng buộc cha mẹ() thay vì cha mẹ() –

+0

Bạn đang tạo đánh dấu không hợp lệ. ID phải là duy nhất. – undefined

Trả lời

4

Hãy thử điều này

$(function() { 
    $('a#disable-link').click(function (e){ 
     var parentDiv=$(this).closest('.added-property-excerpt'); 
     $('.added-property-links').text('loading...'); 
     parentDiv.css("background", "red"); 
     $.ajax({...}); 
     e.preventDefault(); 
    }); 
}); 

Xem sự khác biệt trong a working examplenon-working example.

+3

hoạt động hoàn hảo ... cảm ơn. Nhưng bạn có thể cho tôi biết tại sao mã trước đó không hoạt động? – Tripping

+0

Bạn được chào đón :-) –

+2

@Tripping, tôi nghĩ '$ ('. Added-property-links'). Văn bản ('loading ...');' line đang xóa 'link/ tag' từ dom và '$ (this)' không thể tham chiếu nó sau dòng đó. –

Các vấn đề liên quan