2012-03-14 135 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

你會得到任何javascript錯誤嗎?你有沒有檢查螢火蟲或鉻開發工具 – JIA 2012-03-14 04:23:34

+0

螢火蟲沒有錯誤。自動完成工作正常,但#hiddenId不會改變。 – Devin 2012-03-14 04:26:42

回答

0

對此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

0

下面是關於如何使用自動完成模塊

基本上,你需要添加一個回調時選擇值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); 
     } 
    }); 
相關問題