2011-04-05 38 views
0

我花了很多時間將其他參數添加到Jquery-Autocomplete-Source。但我無法完成。無法將表單值附加到Jquery自動完成源

我在做什麼:我有一個jQuery數據源的自動完成表單。如果您從自動填充中選擇了一些內容,它將被插入到一個表格中(文本和一個帶有ID的隱藏字段),並啓動功能lesen(正常工作)。函數lesen將具有「類」nutzerid(隱藏字段)的每個元素的值讀取到數組並將其放入輸入字段 - 輸入字段是所有id(works!)的收集器。但!自動完成應該讀取該字段的值,並將其作爲參數發送給我的JSON源。但事實並非如此。 Firebug控制檯顯示一個空的參數。即使警報能夠顯示值。我真的不知道,我做錯了什麼。

我也嘗試用一個變量替換輸入字段(所有ID的收集器)。但是同樣的問題發生在這裏。我可以提醒var的值。但試圖將其附加到源代碼會產生一個空的參數。我也嘗試通過param,extraparams選項給自動完成的值。沒有成功。我認爲加入陣列存在問題。可能是lesen中的步驟,其中所有id都被推送到一個數組,並且該數組被填充到表單中。但我不知道如何解決這個問題。

爲什麼我這麼做?我希望JSON源PHP能夠排除其MySQL查詢中已經選擇的ID。以前的請求中選擇的內容不應與自動填充一起顯示。所以我必須發佈,之前添加到請求頁面的內容。這就是我選擇的方式(可能不是最乾淨的)。想象一下購物車。如果產品附加到購物車,則不應在將來的請求中顯示。當然,我接受新的方法。

<input type="text" id="ausschluss" />  
$(document).ready(
function() { 



//this functions reads every hidden field (form the added items) an puts the array as string to a hidden field  
function lesen(){ 
      var itemsarray = []; 
      $(".nutzerid").each(function() { 
      var items = $(this).attr('value'); 
      itemsarray.push(items); 
      }); 
      $("#ausschluss").val(itemsarray); 

      }; 


    //this function attaches the selection (from the autocomplete) to a table it creates text, a hidden field with the user id and a button 
     function log(name, id) { 
      $("<tr> <td>" + name + "<input class='Entf' type='button' value ='Entfernen' />" + "<input type='hidden' class='nutzerid' name='hiddenField' value='" + id + "' /></td></tr>").appendTo("#log"); 
      $("#log").attr("scrollTop", 0); 
      lesen(); 

        } 
//this is the autocompletepart 
     $("#REMOTE").autocomplete({ 
      source: "json.php?aus=" + $("#ausschluss").val(), //this is not working. the firebug console shows an empty param (.php?aus=&term=blabla 
      minLength: 2, 
      select: function(event, ui) { 
       log(ui.item.value,ui.item.id);   
       alert($("#ausschluss").val()); //this works! after selecting an item from the autocomplete it shows me the value of the field "ausschluss", like it should be appended to the source 
       } 

     }); 

//this one captures the click of a button. identified by the class and kills the <tr> <td> and all elemtns in it   
$('.Entf').live('click',function(event){ 
    $(this).parent().parent().remove(); 
    lesen(); 
    }); 




}); 

回答

1

jQueryUI的自動完成控件 - 對話框很像 - 使用$('#myElement').autocomplete()初始化器創建的。

您的問題正在發生,因爲您正在使用$('#ausschluss').val()初始化一次自動填充,但尚未填充該填充。自動完成不會再引用該值 - 它被有效地緩存。你需要重構代碼到ausschluss傳遞給函數的變量,有函數,然後調用自動完成或(可能更好),嘗試設置源上飛象這樣:

$("#REMOTE").autocomplete("option", "source", 'json.php?aus=' + $('#ausschluss').val()); 

你」 d只要你的ausschluss值改變就調用它。

+0

就是這樣!大!非常感謝!!! – brill 2011-04-06 06:57:56

+0

Keine Ursache .. – 2011-04-06 09:11:11

0

我知道現在晚了,但我只是有這個問題,並提出了這個解決方案。

如果你想基於另一個字段中的值的形式,你可以做到這一點有一個自動完成的結果集:

$(document).ready(function(){ 
    $('#alpha').change(function(){setLibraryAutoComplete();}); 
    setLibraryAutoComplete(); 
}); 
function setLibraryAutoComplete(){ 
    $('#beta').autocomplete({source: "TypeAhead?param1=ABC&param2="+$("#alpha").val()}); 
} 

這將設置自動在頁面加載完成從字段中的值但隨後還會導致自動完成調用在源字段值更改時進行更新。