2011-06-05 61 views
1

我试图发送额外的参数与jqueryUI的自动完成使用抽象的方法。正在使用的HTML的一个精简版是:在jquery ui自动完成中访问相对元素?

<form id='employees'> 
    <input class='autocomplete' name='id_number' /> 
</form> 

我需要的是能够找到父相对于任何自动完成输入的ID(因为可能有多个在一个页面上)。我到目前为止的代码是:

$(".autocomplete").autocomplete({ 
     source: function(request, response) { 
      $.ajax({ 
       url: "search.php", 
       data: { 
        form_id: // need help! 
        string: request.term 
       }, 
       success: function(data) { 
        // do stuff 
       } 
      }); 
     } 
    }); 

所以我需要发送的form_id是:

$(this).parent().attr('id') 

但问题是,当使用$(这)内使用一次$ .ajax调用中的源代码函数失去了对原始元素的引用,并且在之前我没有看到任何可以创建引用原始元素的变量的点。

如何获取要作为参数发送的父代的ID?

感谢您的帮助!

回答

0

为什么你不喜欢下面的代码?它可以给你一个想法...

$(".autocomplete").each(function() { 
    var ac = $(this); 
    var parentId = $(this).parent().attr('id');  
    ac.autocomplete({ 
     minLength: 3, 
     source: function(request, response) { 
      findSuggestions(request, response, 'artist', artist_cache, parentId); 
     } 
    }); 
}); 
+0

有趣的'嵌入式'方法我没有考虑 - 也作品!谢谢你,先生。 – thepinnyg 2011-06-05 23:59:36