2013-04-16 3 views
6

x-editable을 사용하여 클릭 한 요소의 "id"를 얻는 방법을 이해하는 데 문제가있어서 x-editable 및 jQuery를 처음 사용합니다. .x-editable 클릭 된 요소의 ID를 얻는 방법

#line_item_unit_cost라는 div 내 페이지에 여러 링크가 있습니다.

<div id="line_item_unit_cost"> 
     <a id="1">link</a> 
     <a id="2">link</a> 
     <a id="3">link</a> 
     <a id="4">link</a> 
     <a id="5">link</a> 
</div> 

링크 중 하나를 클릭하면 인라인 편집을 수행 할 수있는 x 편집 가능한 스크립트가 실행됩니다. 내가 겪고있는 문제는 내가 DB를 업데이트 할 수 있도록 내가 작업중인 광고 항목을 전달해야한다는 것입니다. 내가 클릭하는 링크의 "id"에 액세스하는 방법 (또는 내가 잘못했는지) 모르겠다.

$('#line_item_unit_cost a').editable({ 
      validate: function(value) { 
       if($.trim(value) == '') return 'This value is required.'; 
      }, 
      type: 'text', 
      url: '/post', 
      pk: {{ purchaseOrder.id }}, 
      title: 'Enter Value', 
      params: { 
       purchaseOrderId : {{ purchaseOrder.id }} , 
       lineId : $(this).attr("id"), 
       text: 223 
      }, 
      ajaxOptions: { 
       dataType: 'json' 
      }, 
      success: function(response, newValue) { 

      } 
     }); 

이 행 : lineId : $ (이) .attr ("ID") 나에게 null 값을 제공

여기 내 스크립트입니다.

lineId : $ ("# line_item_unit_cost a"). attr ("id")은 편집중인 페이지가 아닌 페이지의 "id"에서 첫 번째 인스턴스를 계속 가져옵니다.

누구나 내가 x-editable을 사용하여 클릭 한 링크의 ID를 얻는 방법을 알고 있습니까?

감사합니다.

답변

11

내가 ... 다른 사람이 필요한 경우 사람의 게시물을 삭제하는 대신 솔루션을 제공 할 것입니다 결정했다

$('#line_item_unit_cost a').editable({ 
      validate: function(value) { 
       if($.trim(value) == '') return 'This value is required.'; 
      }, 
      type: 'text', 
      url: '/poste', 
      pk: {{ purchaseOrder.id }}, 
      title: 'Enter Freight Value', 
      params: function(params) { 
       var line = $(this).attr("id"); 
       var data = {}; 
       data['purchaseOrderId'] = params.pk; 
       data['field'] = params.name; 
       data['value'] = params.value; 
       data['lineId'] = line; 
       return data; 
      }, 
      ajaxOptions: { 
       dataType: 'json' 
      }, 
      success: function(response, newValue) { 

      } 
     }); 
+0

나는 야 당신이 발견 같은, 좋은 일 제안했다 (그리고 게시를!)를 대답 :) – robertklep

0
$(custom_selector).each(function() { 
    var current_element = $(this); 
    $(this).editable({ 
     //common code where you can use current_element 
    });  
}); 
관련 문제