从jQuery Autocomplete Widget Ajax调用中访问$(this)DOM元素 [英] Access $(this) DOM element from within jQuery Autocomplete Widget Ajax call

查看:152
本文介绍了从jQuery Autocomplete Widget Ajax调用中访问$(this)DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码(Coffeescript)中,在jQuery自动完成数据源的AJAX调用中,在代码的第5行中,我传递了2个参数-term:和ll: 对于ll:我试图将$(this)用作.autocomplete应用于的DOM元素.在这种情况下,其$('[type ="text"] [name * ="[location]"]') 我需要在第5行上专门使用($ this)引用该DOM元素.但是,我相信该范围内的"this"是指不是DOM元素的其他内容.有人可以帮忙解释一下我需要做什么吗?

In the code below (Coffeescript), in the AJAX call for the jQuery autocomplete data source, On line 5 of the code, I am passing 2 params - term: and ll: For ll: I'm trying to get $(this) to be the DOM element that .autocomplete was applied to. In this case its $('[type="text"][name*="[location]"]') I need to reference that DOM element specifically with ($this) on line 5. However, I believe 'this' at that scope refers to something else that isn't a DOM element. Can someone please help explain what I need to do?

$('[type="text"][name*="[location]"]').autocomplete(
    source: (request, response) ->
      $.ajax 
        url: $('[type="text"][name*="[location]"]').data('autocomplete-source')
        data: {term: request.term, ll: $(this).siblings('[name*="[geocode_location]"]')}
        contentType: "application/json; charset=utf-8"
        success: (data) ->
          response $.map(data, (item) ->
            value: item.value
            label: item.label
            address: item.address
          )

    focus: (event, ui) ->
      event.preventDefault()
      $(this).val ui.item.label
    select: (event, ui) ->
      event.preventDefault()
      $(this).val ui.item.label
      $(this).siblings('[name*="[foursquare_id]"]').val ui.item.value
  ).data("autocomplete")._renderItem = (ul, item) ->
    $("<li>").data("item.autocomplete", item).append("<a>" + item.label + "<br>" + item.address + "</a>").appendTo ul

推荐答案

我无法告诉您有关Coffeescript的任何信息,但是 this.element 应该返回元素(它是一个jQuery对象)

I can't tell you anything about Coffeescript, but this.element should return the element(it's an jQuery-object)

应该是:

ll:this.element.siblings('[name*="[geocode_location]"]')

但是这不起作用,因为同级返回一个jQuery对象,并且不能作为请求参数传递.

But this will not work, because siblings returns an jQuery-object and could not be passed as a request-parameter.

这篇关于从jQuery Autocomplete Widget Ajax调用中访问$(this)DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆