Jquery UI 自动完成 - 使用先前选择字段中的值
我想自动完成城市文本输入,但仅搜索先前选择的国家/地区的城市。我的代码似乎应该可以工作,但它没有发送正确的国家/地区值。
形式:
Country: <select name='country' id='country'>
<option value='US'>USA</option>
<option value='UK'>United Kingdom</option>
</select><br/>
City: <input type='text' name='city' id='city' />
js:
$( "#city" ).autocomplete({
source: "searchCities/" + $('select#country option:selected').val(),
minLength: 2
});
URL 结构应为 'searchCities/UK?term=foo',SQL 语句搜索国家代码为 'UK' 的 'term' 值。如果我手动输入 URL,它可以正常工作(仅限于国家/地区)...并且自动完成功能可以在表单中工作。但是,它返回所有城市,不受国家/地区代码限制。
我可能缺少什么吗?或者也许有更好的方法来实现这一目标?谢谢!
I want to autocomplete a Cities text input, but only search for cities in the country that was previously selected. The code I have seems like it should work, but it's not sending the correct country value.
The form:
Country: <select name='country' id='country'>
<option value='US'>USA</option>
<option value='UK'>United Kingdom</option>
</select><br/>
City: <input type='text' name='city' id='city' />
The js:
$( "#city" ).autocomplete({
source: "searchCities/" + $('select#country option:selected').val(),
minLength: 2
});
The URL structure should be 'searchCities/UK?term=foo' and the SQL statement is searching for the value of 'term' where the country code is 'UK'. If I type in the URL manually, it works without a problem (limiting to only the country)... and the autocomplete works in the form. However, it returns ALL cities without limiting by the country code.
Is there something I may be missing? Or maybe a better way to accomplish this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您还没有这样做 - 您应该在每次更改选择框的选定值时修改自动完成的源属性。
每次用户在选择框中选择一个新值时,您都可以销毁并重新构建自动完成功能:
或者最好不要销毁它并重新构建它,而是修改其源属性:
If you are not doing so already - you should modify the autocomplete's source attribute each time the select box's selected value is changed.
you can destroy and re-build the autocomplete every time the user selects a new value into the select box:
or maybe it is better not to destroy it and re-build it, rather modify its source attribute:
试试这个
Try this one