无法将表单值附加到Jquery自动完成源

时间:2011-04-05 23:02:11

标签: jquery autocomplete

我花了很多时间,为Jquery-Autocomplete-Source添加其他参数。但我无法完成它。

我正在尝试做什么:我有一个带有jQuery数据源的自动完成表单。如果从自动完成中选择某些内容,它将被插入到表格中(文本和带有id的隐藏字段)并启动函数lesen(正常工作)。函数lesen将带有“class”nutzerid(隐藏字段)的每个元素的值读取到数组并将其放入输入字段 - 输入字段是所有ID的收集器(有效! )。但!自动完成应该读取此字段的值并将其作为参数发送到我的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();
   });




});

2 个答案:

答案 0 :(得分:1)

jQueryUI自动完成控件 - 与对话框非常相似 - 是使用$('#myElement').autocomplete()初始化程序创建的。

您的问题正在发生,因为您使用尚未填充的$('#ausschluss').val()初始化自动完成功能。自动完成功能不会再次引用该值 - 它会被有效地缓存。您需要重构代码以将ausschluss作为变量传递给函数,然后让该函数调用自动完成或(可能更好),尝试动态设置源代码如下:

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

每当你的ausschluss值发生变化时,你就会调用它。

答案 1 :(得分:0)

我知道现在已经很晚了,但我刚遇到这个问题,并提出了这个解决方案。

如果您希望根据表单中其他字段的值设置自动填充结果集,则可以执行以下操作:

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

这会使用字段中的值设置页面加载时自动完成,但是也会在源字段值更改时随时更新自动完成调用。