我正在尝试开发一个用PHP编写的网站,其中包括一个用户使用它在网站上注册的页面。在选择此页面的国家/地区部分时,我尝试使用 LOOPJ (http://loopj.com/jquery-tokeninput/demo.html) jQuery 自动完成脚本。当我尝试搜索并制作建议列表时,我想发送位于表单文本框中的另一个字段的数据。所以我尝试使用以下代码:
$(document).ready(function() {
var countryfrom = $("input#country2").val();
$("#city-input-custom-limits").tokenInput("autocomplete/city_search.php?country=" + countryfrom, {
searchDelay: 1000,
minChars: 3,
tokenLimit: 1
});
});
一切正常。但是当我更改"country2"文本框值时,countryfrom
的值不会更改,country2
的初始值返回到city_search.php
页面。
谁能提出为什么countryfrom
值在country2
改变时没有改变的建议?
你应该
听keyup
事件:
// Cache the element
var $elem = $("#city-input-custom-limits");
$("#country2").on('keyup', function() {
var country = $.trim(this.value);
$elem.tokenInput("autocomplete/city_search.php?country=" + country, {
searchDelay: 1000,
minChars: 3,
tokenLimit: 1
});
}).triggerHandler('keyup'); // execute the handler on DOM ready