jQuery的自动完成功能选择:功能不工作

问题描述:

这看起来很简单,但我似乎无法得到它的工作: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

你会得到任何javascript错误吗?你有没有检查萤火虫或铬开发工具 – JIA 2012-03-14 04:23:34

+0

萤火虫没有错误。自动完成工作正常,但#hiddenId不会改变。 – Devin 2012-03-14 04:26:42

对此code..hope看看它会帮助ü

$('#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

Firebug给我一个错误,说$('#fieldWithData')。autocomplete不是一个函数。 – Devin 2012-03-14 04:43:08

+0

chck this link u will get idea ..... http://www.petefreitag.com/item/756.cfm – 2012-03-14 05:02:34

下面是关于如何使用自动完成模块

基本上,你需要添加一个回调时选择值full blog entry被改变,像这样:

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); 
     } 
    });