【发布时间】:2011-09-18 05:53:58
【问题描述】:
首先让我说我是 Ajax 的新手。我一直在阅读来自 jquery.com 的文章和一些教程,但我还没有弄清楚这对我想要实现的目标是如何工作的。
我正在尝试使用 Google 的 Weather API XML 获取搜索城市的天气,无需刷新页面。
我设法检索天气 XML 并解析数据,但每次我搜索不同的地方时,页面都会重新加载,因为我的天气小部件位于选项卡下。
这是我的 HTML 中的内容:
<script type="text/javascript">
$(document).ready(function(){
// FOR THE TAB
$('.tab_btn').live('click', function (e) {
$('.tab_content').fadeIn();
});
$(".submit").click(function(){
$.ajax({
type : 'post',
url:"weather.php",
datatype: "text",
aysnc:false,
success:function(result){
$(".wedata").html(result);
}});
});
});
</script>
<style>.tab_content{display:none;}</style>
</head><body>
<input type="button" value="Show Content" class="tab_btn">
<div class="tab_content">
<h2>Weather</h2>
<form id="searchform" onKeyPress="return submitenter(this,event)" method="get"/>
<input type="search" placeholder="City" name="city">
<input type="hidden" placeholder="Language" name="lang">
<input type="submit" value="search" class="submit" style="width:100px">
</form>
<div id="weather" class="wedata">
</div>
</div>
这是我正在处理的实际演示:http://downloadlive.org。
现在,如果我在搜索表单上添加action="weather.php",我会得到结果,但我会被重定向到weather.php,这是合乎逻辑的。如果没有action="weather.php",每次我搜索我所在的索引时,都会加起来/?city=CITY+NAME,这不应该。这应该添加到weather.php,获取结果,然后将它们检索回我的索引中,如果有意义的话?
这是我的 weather.php 的 php 代码:http://pastebin.com/aidXCeQg
可以在这里查看:http://downloadlive.org/weather.php
有人可以帮我解决这个问题吗?
非常感谢
【问题讨论】: