我正在尝试使用AJAX发布表单数据并在div中显示结果。尽管我尽了最大的努力,但我失败了。jQuery表单提交和显示结果不起作用
这里是我与jQuery使用的代码1.8.3
HTML:
<form action="" method="post" id="forecastform">
<input type="text" name="weatherloc" class="weatherloc">
<input type="submit" name="weathersubmit" class="weathersubmit" value="Get Forecast">
</form>
<div class="wfposts"></div>
的JavaScript在头部分:
<script type="text/javascript">
jQuery('#forecastform').submit(function(event) {
event.preventDefault();
var term = jQuery('.weatherloc').val();
var url = <?php echo get_template_directory_uri() . '/local-forecast-process.php';?>;
var posting = jQuery.post(url, { weatherloc: term });
posting.done(function(data) {
var content = jQuery(data);
jQuery(".wfposts").empty().append(content);
});
});
</script>
首先,页面重新加载忽略event.preventDefault();
我不确定是否正在发送数据,因为在页面加载后div未被填充。
有人请帮助
谢谢,它的工作..对不起,再次打扰,有没有什么办法可以添加ajax加载图像与此代码? – Abhik
@Abhik - 当然,像这样 - > http://jsfiddle.net/Jnm8A/ – adeneo
真棒!你让我今天一整天都感觉很好。 – Abhik