我必须向某些站点添加一些具有自动完成功能的 html 表单。我已经使用 jqueryui 的自动完成功能完成了很多次,但这次出了点问题。情况应该是这样的:
- 文本字段 - 选择城市(仅来自自动完成功能的值有效)
- 文本字段 - 从该城市选择街道(同样它是取决于第一个的自动完成字段。
因此,出于这个原因,我创建了一个变量来保存第一个值并将其传递给第二个值,但它并不完全如此。我正在通过调试发布我的代码并对其进行评论:
<script type="text/javascript">
var city = "";
var num = 0;
jQuery(document).ready(function() {
jQuery("#city").autocomplete({
source: "<?php echo $url; //eg http://sitename.tld/somecontroller/suggestCity ?>",
minLength: 2,
select: function( event, ui ) {
if(ui.item)
{
window.num++;
jQuery("#city_selected").html(ui.item.value); //this alerts correctly
jQuery("#street").removeAttr('disabled');
window.city = ui.item.value.toString();
alert(window.city); // this alerts correctly
}
}
}); // this executes correctly - the autocomplete works and the second field becomes enabled
var streetUrl = "<?php echo $url; //eg http://sitename.tld/somecontroller/suggestStreet ?>/"+window.city+"/"; /* so this has to become http://sitename.tld/somecontroller/suggestStreet/$city/ where city is given from the first autocomplete */
jQuery("#street").autocomplete({
source: streetUrl,
minLength: 2,
select: function( event, ui ) {
if(window.num == 0)
return;
if(ui.item)
{
window.num++;
jQuery("#street_selected").html(ui.item.value);
}
}
}); // when this executes firebug tells me the url is http://sitename.tld/somecontroller/suggestStreet//?term=...... and here is the problem. There are two forwarding slashes that tells me the concatenation isn't ok.
});
function dump() // I created this function for debuging purposes and i attached it to button with onclick="javascript: dump(); "
{
alert(window.city); // this displays correctly
alert(window.num); // this displays correctly
}
</script>
提前致谢。