在自动完成AJAX请求中传递参数

时间:2019-04-10 09:56:33

标签: jquery jquery-ui jquery-ui-autocomplete

我正在研究要集成的自动完成选项。自动完成功能正常。但是,当我添加另一个参数变量时,自动完成功能不起作用,请猜测还存在语法问题。在下面的脚本中,我需要将可变的国家/地区代码传递给fetch_customers.php

$(document).ready(function($) {
  $("#customers").autocomplete({
    var countrycode = '<?php echo $agencyid; ?>';
    data: {
      countrycode: countrycode
    },
    source: "fetch_customers.php",
    minLength: 2,
    select: function(event, ui) {
      var url = ui.item.id;
      if (url != '#') {
        location.href = '/view-customer/' + url;
      }
    },
    // optional (if other layers overlap autocomplete list)
    open: function(event, ui) {
      $(".ui-autocomplete").css("z-index", 1000);
    }
  });
});

1 个答案:

答案 0 :(得分:1)

您的语法无效。您需要在提供给countrycode的对象之外定义autocomplete()

话虽如此,这不是您如何在jQueryUI自动完成请求中传递数据的方式。相反,您需要在调用的URL的查询字符串中传递值:

$(document).ready(function($) {
  $("#customers").autocomplete({
    source: "fetch_customers.php?countrycode=<?php echo $agencyid; ?>",
    minLength: 2,
    select: function(event, ui) {
      var url = ui.item.id;
      if (url != '#') {
        location.href = '/view-customer/' + url;
      }
    },
    open: function(event, ui) {
      $(".ui-autocomplete").css("z-index", 1000);
    }
  });
});