2017-04-13 1 views
1

JQuery에 문제가 있습니다. 사용자가 자동 ​​완성 드롭 다운 메뉴 <input class="itemCode">에서 항목을 선택한 후에 <input class="itemDescription">을 채워야합니다. 사용자가 item을 선택할 때만 JQuery가 제대로 작동하며 1 행과 2 행의 입력에 모두 동일한 Item Description이 채워집니다. 나는 그 두 가지 모두가 동일한 클래스를 가지고있는 itemDescription 입력이라고 생각한다. 그러나, 나는 그것을 고치는 방법을 모르겠다. 어떤 도움이라도 대단히 감사하겠습니다!JQuery 자동 완성은 선택시 모든 행을 채 웁니다.

<table class="table"> 
    <thead> 
     <tr> 
     <td >Item Code</td>    
     <td >Description</td> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td class=""><input class="itemCode"></td>    
     <td class=""><input class="itemDescription"></td> 
    </tr> 

    <tr> 
     <td class=""><input class="itemCode"></td>    
     <td class=""><input class="itemDescription"></td> 
    </tr> 
    </tbody> 
</table> 


<script> 
    $(function() { 
    $(".itemCode").autocomplete({ 
     source: function(request, response) { 
     $.ajax({ 
      url: "item-search.php", 
      dataType: "json", 
      data: { 
      term: request.term 
      }, 
      success: function(data) { 
      response(data); 
      } 
     }); 
     }, 
     minLength: 1, 
     select: function(event, ui) { 
      $(".itemDescription").val(ui.item.label); 
     } 
    }); 
    }); 
    </script> 

답변

1

주어진 컨텍스트를 사용하여 올바른 입력을 참조하십시오. 당신은 당신이 추가 할 수있는 모든 필드에 대해 작업하려면

$(this).parent().next().children(".itemDescription").val(ui.item.label); 

는 다음을 사용 :

난 그냥 $ '로 변경
$(this).parent().siblings().children(".itemDescription").val(ui.item.label); 
+0

은 매력처럼 작동합니다! 감사! – Luke

+0

'itemPrice'와 같은 추가 필드를 추가하면 구문이 어떻게 생겼을까요? 이것을 시도하면 : $ (this) .parent(). next(). children (". itemDescription") .val (ui.item.itemDescription); \t $ (this) .parent(). next(). children (". itemCost") .val (ui.item.itemCost); ' 새로운 필드'itemPrice'는 채워지지 않습니다. 감사! – Luke

+0

'.next()'를 추가하거나 편집 된 솔루션을 사용할 수 있습니다. 나는 좀 더 일반적인 것처럼 편집 된 솔루션을 제안 할 것이다. –

0

검색된 클래스의 첫 번째 인스턴스에 결과를 제시하면됩니다.

$(".itemDescription").val(ui.item.label); 

은 해당 클래스의 모든 요소를 ​​해당 값으로 설정합니다. 첫 번째 설정 만하려는 경우

$(".itemDescription").first().val(ui.item.label); 
+0

(".itemDescription") 좁은 방 다음을 시도() .val (ui.item.label);하지만 이제는 맨 처음에만 채워집니다. – Luke

+0

대신에'this'을 사용해야합니까? – Luke

+0

jQuery에서 '현재 입력 필드'의 색인 (위치)을 가져 와서 해당 설명 필드를 대상으로 색인을 사용하는 방법을 찾으려고합니다. – tamak