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

问题描述 投票:0回答:1

我正在开发一个自动完成选项进行集成。自动完成工作正常。但是当我添加另一个参数变量时,自动完成功能无效,猜猜有点语法问题。在下面附带的脚本中,我需要将变量countrycode传递给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);
    }
  });
});
jquery jquery-ui jquery-ui-autocomplete
1个回答
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);
    }
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.