2012-03-14 6 views
0

을 작동하지 않는,하지만 난 일하러 수없는 것 : 항목이 자동 완성 목록에서 선택jQuery를 자동 완성 선택 : 기능이 매우 간단한데

var options, a; 
jQuery(function(){ 
    options = { 
    serviceUrl:'ajax/parts_by_partno.php', 
    select: function(event, ui) { 
    $("#hiddenId").val(ui.item.id); 
    } 
    }; 
    a = $('#fieldWithData').autocomplete(options); 
}); 

되면, 내가 #hiddenId 변경하려는 그 선택한 항목의 ID에 값 :

$("#hiddenId").val(ui.item.id); 

심지어 그냥이 같은 정적 뭔가 #hiddenId을 변경 시도했다 :

$("#hiddenId").val('test'); 

변경된 사항이 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

어떤 자바 스크립트 오류가 있습니까? 당신이 방화범이나 크롬 dev에 도구를 체크 했습니까 – JIA

+0

방화범에 오류가 없습니다. 자동 완성 기능이 제대로 작동하지만 #hiddenId가 ​​변경되지 않았습니다. – Devin

답변

0

는 U 여기

$('#selector').autocomplete({ 
    source: url, 
    select: function (event, ui) { 
     $("#txtAllowSearch").val(ui.item.label); // display the selected text 
     $("#txtAllowSearchID").val(ui.item.value); // save selected id to hidden input 
    } 
}); 

$('#button').click(function() { 
    alert($("#txtAllowSearchID").val(); // get the id from the hidden input 
}); 
+0

방화범이있어서 $ ('# fieldWithData')라고 말하는 오류가 발생합니다. 자동 완성 기능이 아닙니다. – Devin

+0

이 링크를 클릭하면 아이디어를 얻을 수 있습니다. ..... http://www.petefreitag.com/item/756.cfm –

0

는 기본적으로, 당신은 콜백을 추가 할 필요가 자동 완성 모듈을 때 선택 값을 사용하는 방법에 대한 full blog entry입니다 도움이 code..hope에 봐 다음과 같이 변경되었습니다.

select: function(e, ui) { 

     //create formatted friend 
     var friend = ui.item.value, 
      span = $("<span>").text(friend), 
      a = $("<a>").addClass("remove").attr({ 
       href: "javascript:", 
       title: "Remove " + friend 
      }).text("x").appendTo(span); 

      //add friend to friend div 
      span.insertBefore("#to"); 
     }, 

     //define select handler 
     change: function() { 

      //prevent 'to' field being updated and correct position 
      $("#to").val("").css("top", 2); 
     } 
    }); 
관련 문제