2012-05-02 43 views
-2

我有一个aspx页面和一个asp文本框控件,它具有ajax autoCompleteExtender。我希望根据自动完成列表中选定的元素将页面重定向到其他页面。但是当我使用window.location()在.aspx页面不工作

window.location() 

什么都没有发生,只是刷新相同的页面。这是我的javascript;

<script type="text/javascript"> 
    function selectCity() { 
     var str = document.getElementById('<%= txtSearchForDestination.ClientID %>').value; 
     var array = str.split(","); 
     var city = array[0].replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 
     city = city.replace(/ /g, "+") 
     var country = array[1].replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 
     country = country.replace(/ /g, "+") 
     window.location.href("City.aspx?city=" + city + "-" + country); 
    } 
</script> 

的脚本工作,我想它像

警报( “City.aspx城市=?” +城市+ “ - ” +国家)

有没问题。但是当我想重定向到该页面时,它不起作用。我也试过

window.location的( “http://www.google.com”)

它不工作为好。

可能是什么问题?

+3

**在您的JavaScript控制台中阅读** [文档](https://developer.mozilla.org/en/DOM/window.location)和** look **。 'location'不是一个函数。 'location.href'不是一个函数。对于这些查询参数,您可能还需要使用['encodeURIComponent'](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent),并且没有替换('/','' +')'虚弱。 –

回答

1

你试过:

window.location = 'City.aspx?city=' + city + '-' + country; 

+0

是的,没有什么改变。 – Mtok

+0

你确定你完全像我的答案中的代码吗? –

+0

omg是的,它现在工作!我正在尝试做一个小时的判断!谢谢。 – Mtok

2

这不是一个函数,它是一个属性。

window.location.href = "City.aspx?city=" + city + "-" + country; 
+0

谢谢,当然..我认为这是工作太多时间的结果。 – Mtok

相关问题