我知道它是回来经常在很多帖子,但即使阅读几十个答案后一个问题,我仍然无法弄清楚什么是错在我的代码。防止默认事件 - jQuery的
关键是要防止默认提交,并在回答DIV检索响应数据。代码实际上做的是直接将我发送到geocoder.php页面。
非常感谢,
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
/* attach a submit handler to the form */
$("geostring").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $(this),
term = $form.find('input[name="geo"]').val(),
url = $form.attr('action');
/* Send the data using post */
var posting = $.post(url, { s: term });
/* Put the results in a div */
posting.done(function(data) {
var content = $(data).find('#content');
$("#answer").empty().append(content);
});
});
</script>
<form action="http://winefy.alwaysdata.net/geocoder.php" method="POST" id="geostring">
<input type=text name="geo" placeholder="Address..." />
<input type="submit" value="Geocode" />
<div id="answer"></div>
</form>
@Crowder Isin't只能通过加载所有的HTML元素之后执行的脚本浏览器? –
@Vivek:不,脚本一遇到'script'标签就立即执行。 –
非常感谢您的回答。我只是做了变化,但它不工作... 我用这个代码的自定义组件的一个Joomla视图,它改变什么? – justberare